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
3 Answers
Reset to default 4It'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 function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742289558a2447544.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论