0

Lets say I have the following js code

$(function() {
   $('#some_id').load('myfile.html', function() {
      alert('Call back called');
   });

  alert('You should alert second');
  // more javascript code .........

});

For some reason, alert('Call back called'); is the last thing that gets alerted to me. I thought that js code executes in the order of the file. Is there a way to make alert('Call back called'); go first before all my other functions.

Looking at my simple code snippet one would suggest why not put alert('You should alert second'); in the function call back, unfortunately I am inheriting a code base that is really big and jamming all those function calls in my callback wont be feasible

6
  • 9
    Welcome to the wonderful world of async! You can't do that. Commented Aug 27, 2013 at 14:58
  • 1
    You can use promises to make this easier. Commented Aug 27, 2013 at 14:58
  • 2
    alert('You should alert second'); is called immediately when the js is initialized, where the alert('Call back called') will be called only after the .load is called (look into callbacks). Commented Aug 27, 2013 at 14:59
  • the callback function of the load() function is thought exactly for this purpose, you should put the code there, there aren't reasons to don't do that. Commented Aug 27, 2013 at 15:00
  • 1
    You could use .ajax() instead of .load() and set async: false like explained here: stackoverflow.com/questions/133310/… Commented Aug 27, 2013 at 15:03

6 Answers 6

2

.load() is an asynchronous method, and the anonymous function you passed to it as a parameter is executed ONLY when the asynchronous method finishes executing.

Sign up to request clarification or add additional context in comments.

Comments

1

"I thought that js code executes in the order of the file." It does, and that's what's happening here. But because the .load() callback function is, well, a callback (i.e. only fires once the asynchronous operations is complete) alert('Call back called'); will only fire when it's invoke at some point in the future.

1 Comment

Here's a basic demo of the concept you're explaining pastebin.com/1zSt0vH2
1

As pointed by others, load is an async method, hence you need to use something like a synchronous AJAX call. Use it this way:

$.ajax({
     url:    'myfile.html',
     async:   false
     success: function(result) {
                  alert('Call back called');
              }
});

alert('You should alert second');

2 Comments

I agree. If you downvote something please leave a comment. This seems like a good way to replace the asynchronic method, worth a try.
I guess the down vote because it did not work. I tried but had no luck with your solution.
1

Using pure javascript, just add a function with a callback argument for the stuff coming in the second alert() area:

$(function() {

   function someCode (callback) {
     $('#some_id').load('myfile.html', function() {
       alert('Call back called');
       // add parameters, if you like
       callback();
     });
   }

   function someMoreCode() {
     alert('You should alert second');
     // more javascript code .........
   }

   // And Finally...
   someCode(someMoreCode);

});

3 Comments

Nice, but you should call callback inside load's callback. Moreover, you don't have to wrap the load call inside a function, just declare callback somewhere and use it.
1. Why? 2. Example? It would be helpful if you explained your points beyond stating what should be done and what not. I actually liked your solution, but wanted to give one which works without jQuery. So why vote down?
Your code produces exactly the same result than his code. That's it.
1

You could use the Deferred Object which deals with asynchronous calls for you. Doing so your code will be executed in the reading order, no matter when the response is sent back :

$(function() {
    var jqxhr = $.get('myfile.html', function(data) {
        $('#some_id').html(data);
        alert('Call back called');
    });
    jqxhr.done(function() { 
        alert('You will alert second');
    });
});

Comments

0

If you want alert('You should alert second'); to happen second you need to call it from within your callback function:

$(function() {
   $('#some_id').load('myfile.html', function() {
      alert('Call back called');
      alert('You should alert second');
   });

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.