是否有代码可用于在图像未加载时提醒用户?

Is there code that can be used to alert user when image does not load?

我有一张从服务器加载的图片。我正在通过以下代码执行此操作。

img.onload = function () {
   loadingStop = true;
   ctx.drawImage(img, 10, 10);
   startDrawing();
}
img.src = imageLink;

图像加载后,程序将继续运行。但是,万一照片无法加载,我想 alert(msg) 问题的用户。我如何使用 JavaScript 执行此操作?

只需使用onerror

<img src="../missing-file.jpg" onerror="myAlertFunction()">
function myAlertFunction() {
  alert('The image could not be loaded.');
}