Home > Software engineering >  Wait for .load to finish and then do something
Wait for .load to finish and then do something

Time:02-10

I'm loading an external script through .load('url') and need to wait for it to finish so I can update the source of an image. Sometimes load is fast enough that the image reload applies, but most of the time it takes too long and the image reload happens first, so the image doesn't actually change.

CodePudding user response:

Add a callback function:

$('#yourElement').load('yourUrl.html', function() {
  /* When load is done */
});

More to read at http://api.jquery.com/load

CodePudding user response:

load method has callback function that is executed when the request completes. I think you should use it.

CodePudding user response:

Your question is answered here on how to capture the success event of a load function: Checking Jquery ajax load success

  •  Tags:  
  • Related