admin管理员组

文章数量:1332389

How can I call an asynchronous function and then create a pause in the code that follows the call to the function so that I may be (almost) sure that the asynchronos function has finished?
I don't want to put the code that follows the call inside a function and then delay it to achieve this, just pause the code as it is for a few seconds.

Here is what I mean:

<script>
   asynchronousFunction(); // start running immediatly

   waitFor10Seconds(); // only the following code should wait while the async 
                       // function is running in the background

   rest of the code; // this code will start running after 10 seconds have passed 
                     // since the async function has been called     
</script>

How can I call an asynchronous function and then create a pause in the code that follows the call to the function so that I may be (almost) sure that the asynchronos function has finished?
I don't want to put the code that follows the call inside a function and then delay it to achieve this, just pause the code as it is for a few seconds.

Here is what I mean:

<script>
   asynchronousFunction(); // start running immediatly

   waitFor10Seconds(); // only the following code should wait while the async 
                       // function is running in the background

   rest of the code; // this code will start running after 10 seconds have passed 
                     // since the async function has been called     
</script>
Share Improve this question asked May 18, 2011 at 19:13 AshAsh 1,3093 gold badges17 silver badges25 bronze badges 3
  • I've tried it and had many plications that I've asked about here and no one was able to find their source. Can you please say how to do it without a callback? – Ash Commented May 18, 2011 at 19:18
  • No, you want a callback. Do it right, and figure out the error, rather than doing it wrong. Your way will work "some times" and is absolutely the wrong way to do it. – user229044 Commented May 18, 2011 at 19:46
  • @meagar, of course doing it right is the way to go, but I've been trying to find the problem for almost three weeks now and no can help me with that (many have tried) and I'm out of options. – Ash Commented May 19, 2011 at 7:08
Add a ment  | 

3 Answers 3

Reset to default 4

It's called setTimeout

asyncThing();
setTimeout(function() {
    // do stuff
}, 10000);

Ideally though the async operation should allow you to pass a callback so you turn it into

asyncThing(function() {
    // do stuff
}); 

As mentioned, you should really use a callback. It's easy with jQuery:

$.get("page.php", "key1=value1", function(data) {
    // Code in here will be executed when response has been received
});

http://api.jquery./jQuery.get/

You can of course use $.post() if you'd rather POST the data.

Aldo a callback is better practice, this what you asked for

window.setTimeout(function(){ ... }, 10000);

本文标签: How to create a pause in javascriptjquery code that follows a call to asynchronous functionStack Overflow