How to cancel a jquery.load()?

前端 未结 7 1023
清酒与你
清酒与你 2020-11-29 07:30

I\'d like to cancel a .load() operation, when the load() does not return in 5 seconds. If it\'s so I show an error message like \'sorry, no picture loaded\'.

What I

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-29 08:02

    $('#myImage').load(function(){...}) is not a function call to load the image, it is actually shorthand to bind a callback to the onload event.

    Therefore, adding a timeout parameter to the .load() method as suggested in other answers will have no effect.

    It think your two options are:

    1. Continue on the path you are following and do something like $('#myImage').attr('src', ''); to cancel the image load after it times out, or

    2. Find some way to use $.ajax( { ... , timeout: 5000, ...} ); to load the image instead of letting the browser do it automatically via the attribute.

提交回复
热议问题