如何将jQuery代码暂停几毫秒? [英] How to pause jQuery code for few milliseconds?

查看:90
本文介绍了如何将jQuery代码暂停几毫秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery Ajax函数通过cron自动更新数据库.由于有很多行需要更新,因此我想在每次迭代时将代码暂停几毫秒.最好的方法是什么?

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?

这是我的代码示例:

<?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";

?>

推荐答案

只有两种方法可以做到这一点:

There are only two ways to do this:

  1. 使用setTimeout(例如10毫秒):

  1. 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循环(这是一个技巧,因此不推荐使用):

  • For loop (this is a hack, so this is not preferred):

    for(i = 0; i < 500; i++);

    这篇关于如何将jQuery代码暂停几毫秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆