Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Is there a way to force next line of code to run after jquery ".load completes"

Posted on 2011-09-19
3
Medium Priority
?
229 Views
Last Modified: 2012-06-27
I am using "$("divIdName").load()" function to populate a div element.  For some reason, I seem to be having a timing issue where the subsequent lines of code seem to be executed before the load completes.  Is there a way I can force the load to complete before the subsequent lines of code are executed?
0
Comment
Question by:rawcoder
3 Comments
 
LVL 83

Expert Comment

by:leakim971
ID: 36560122
use : $.ajaxSetup({"async":false});

$.ajaxSetup({"async":false});
$("divIdName").load(.............. // your current code here
$.ajaxSetup({"async":true});

Open in new window


or use the full ajax : $.ajax({ "async":false, "url":"the_url_youve_in_load", function(data) { $("divIdName").html( data ); } });
0
 
LVL 11

Accepted Solution

by:
JohnSixkiller earned 2000 total points
ID: 36565217
Hi

you are using AJAX (Asynchronous Javascript and XML). So every request you make is served in background and any following code is executed immediately after the request. This approach will prevent browser to freeze if server response (for any reason) is delayed.

You have two options:

1) Make synchronous request - If you are sure that server response will not take long
use leakim9871s code or
var html = $.ajax({
  url: "your_url",
  async: false
 }).responseText;
$("divIdName").html(html);

Open in new window

See: http://api.jquery.com/jQuery.ajax/

2) Make it really asynchronous
$("divIdName").load("your_url",function(){
  // The rest of your code goes here. It will be executed AFTER request is completed
 ...
});

Open in new window

See: http://api.jquery.com/load/
0
 
LVL 6

Expert Comment

by:jjperezaguinaga
ID: 36570334
Greetings rawcoder,

The best way to solve your issue is to handle whatever's next your load with a function. JohnSixKiller second option is the way to go.

Best regards,
-JJ
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question