search

Home  >  Q&A  >  body text

javascript - What is the difference between Img.complete and img.onload to determine when the image is loaded?

What is the difference between the two to determine whether the image is loaded? Generally, the onload event will be called back when the loading is completed. Is img.complete still necessary?

PHP中文网PHP中文网2755 days ago1134

reply all(4)I'll reply

  • 高洛峰

    高洛峰2017-06-28 09:24:52

    img.complete is a property of the Image object and returns a Boolean value; img.onload is an event triggered after the image is loaded

    reply
    0
  • PHP中文网

    PHP中文网2017-06-28 09:24:52

    Of the two, only img.complete can determine whether the image has been loaded. img.onload cannot determine whether the image has been loaded. Instead, after the loading is completed, the function bound to onload is directly run.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-06-28 09:24:52

    complete is just an attribute of the HTMLImageElement object, while onload is the load event callback of the Image object. The former cannot accurately perform asynchronous callbacks when the event occurs and has some issues with browser compatibility.

    reply
    0
  • 黄舟

    黄舟2017-06-28 09:24:52

    The onload property of the GlobalEventHandlers mixin is an event handler for the load event of a Window, XMLHttpRequest, <img> element, etc., which fires when the resource has loaded.
    The onload event will be released when img is loaded, src The image is loaded asynchronously. If the loading is completed before the binding event, the onload event will not be triggered.
    img.complete is a property that always exists and is true after loading is complete.

    reply
    0
  • Cancelreply