Javascript 如何暂停 jQuery 代码几毫秒?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5556971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pause jQuery code for few milliseconds?
提问by Mike
I'm using jQuery Ajax functions to auto update my database through cron. Since there are a lot of rows to be updated, I'd like to pause the code for few milliseconds each iretation. What would be the best way to do it?
我正在使用 jQuery Ajax 函数通过 cron 自动更新我的数据库。由于有很多行要更新,我想在每次迭代时暂停代码几毫秒。最好的方法是什么?
Here's sample of my code:
这是我的代码示例:
<?php
$zdroje = $db->select('zdroje', 'id!=1');
echo "<script type='text/javascript'>\n
$(document).ready(function() {\n";
foreach($zdroje as $zdroj) {
echo "$.post( '/adminator/menu/2zajezdy/tools/01otnXml/requests.php', { 'updateXML': '".$zdroj['id']."' }, function(data) {
// pause here!
});\n";
} // end: foreach
echo "});\n</script>\n";
?>
回答by beatgammit
There are only two ways to do this:
只有两种方法可以做到这一点:
Use setTimeout (for example, 10 milliseconds):
setTimeout(function () { $.post( '/adminator/menu/2zajezdy/tools/01otnXml/requests.php', { 'updateXML': '".$zdroj['id']."' }, function(data) { // do stuff here! }); }, 10);
For loop (this is a hack, so this is not preferred):
for(i = 0; i < 500; i++);
使用 setTimeout(例如,10 毫秒):
setTimeout(function () { $.post( '/adminator/menu/2zajezdy/tools/01otnXml/requests.php', { 'updateXML': '".$zdroj['id']."' }, function(data) { // do stuff here! }); }, 10);
For 循环(这是一个 hack,所以这不是首选):
for(i = 0; i < 500; i++);
回答by Shakakai
I suggest you take a look at jQuery's new defer system. Here's a good tutorial: http://www.erichynds.com/jquery/using-deferreds-in-jquery/
我建议你看看 jQuery 的新延迟系统。这是一个很好的教程:http: //www.erichynds.com/jquery/using-deferreds-in-jquery/
Essentially, you can create a "hold" promise like this:
本质上,您可以像这样创建一个“保留”承诺:
function hold(delay){
var dfd = $.Deferred();
setTimeout(function(){
dfd.resolve();
}, delay);
return dfd.promise();
}
Then string together ajax requests with it like this:
然后像这样将ajax请求与它串在一起:
$.when($.post('yourLongUrlHere'))
.then(hold(500))
.then($.post('anotherUrl'))
.then(hold(500))
.then($.post('somethingElse.php'));
This will make each ajax request in order waiting 500 miliseconds in between each.
这将使每个 ajax 请求依次等待 500 毫秒。
Should handle what you asked about w/o a problem.
应该处理您询问的有关 w/oa 问题的问题。
回答by stephen776
you can try the .delay() function...
你可以试试 .delay() 函数...
回答by Phil.Wheeler
A clumsy approach might arguably be to use JavaScript's setTimeout() method, but I'd recommend you look into the jQuery functions, $.ajaxComplete(), $.ajaxStart()and $.ajaxStop().
一种笨拙的方法可以说是使用 JavaScript 的 setTimeout() 方法,但我建议您查看 jQuery 函数,$.ajaxComplete()、$.ajaxStart()和$.ajaxStop()。
回答by zindel
I suppose you'd want to generate the chained call instead of plain list. I.e. what you get now is:
我想你想生成链式调用而不是普通列表。即你现在得到的是:
$.post(...)
$.post(...)
...
$.post(...)
You'd want to get something like this:
你想要得到这样的东西:
$.post(url1, function(data) {
setTimeout(function() {
$.post(url2, function(data) {
setTimeout(function() {$.post(url3)}, 500);
});
}, 500);
});
Having that you're using PHP to generate the JavaScript code - it shouldn't be too difficult to produce the code like this. Hope this helps.
既然您正在使用 PHP 来生成 JavaScript 代码 - 生成这样的代码应该不会太难。希望这可以帮助。
Edit: Try generating it like this
编辑:尝试像这样生成它
$code = "%s";
foreach($sources as $source) {
$part = "$.post( '/adminator/menu/2zajezdy/tools/01otnXml/requests.php', { 'updateXML': '${source['id']}' }, function(data) {
setTimeout(function() {
%s
}, 500);
});"
$code = sprintf($code, $part);
}
$code = sprintf($code, '');
回答by John Strickler
You can't pause the JavaScript engine from processing code. JS has code that runs asynchronously - for example, a response from an AJAX request comes back and the callback function is executed.
您无法暂停 JavaScript 引擎处理代码。JS 具有异步运行的代码 - 例如,来自 AJAX 请求的响应返回并执行回调函数。
setTimeout is your best friend in regards to delaying the execution of a particular function.
在延迟特定函数的执行方面,setTimeout 是您最好的朋友。
//Executes an alert exactly 1 second later
setTimeout(function() {
alert('hello world');
}, 1000);