在一个jQuery / AJAX功能设置延迟功能、jQuery、AJAX

2023-09-11 00:36:16 作者:稍微。

我想延迟添加到重复的查询。

I am trying to add a delay to a repeatable query.

我发现.delay不是一个在这里使用。相反,我应该去的setInterval或setTimeout的。我都试过,没有任何运气。

I found out that .delay is not the one to use here. Instead, I should go with setInterval or setTimeout. I tried both, without any luck.

下面是我的code:

<?php 
include("includes/dbconf.php");

$strSQL = mysql_query("SELECT workerID FROM workers ORDER BY workerID ASC");
while($row = mysql_fetch_assoc($strSQL)) {
?>
<script id="source" language="javascript" type="text/javascript">

  $(setInterval(function ()
  {
    $.ajax({                                      
      cache: false,
      url: 'ajax2.php',        
      data: "workerID=<?=$row['workerID'];?>",
      dataType: 'json',    
      success: function(data)
      {
        var id = data[0];              //get id
        var vname = data[1];           //get name
        //--------------------------------------------------------------------
        // 3) Update html content
        //--------------------------------------------------------------------
        $('#output').html("<b>id: </b>"+id+"<b> name: </b>"+vname);
      } 
    });
  }),800); 

  </script>
<?php
}
?>
<div id="output"></div>

在code正常工作,它输出的结果作为问道。这只是加载没有延迟。该timout和/或间隔似乎并没有工作。

The code works fine, it outputs the result as asked. It's just loads without the delay. The timout and / or interval doesn't seem to work.

任何人都知道我在做什么错了?

Anybody knows what I am doing wrong?

推荐答案

我永远无法理解为什么人们总是加入间隔的AJAX请求,而不是让成功的Ajax调用只是自称,所有的,而冒着严重的服务器通过负载多次请求,不只是拨打另一个电话,一旦你有一个成功的回来。

I've never understood why people always add their AJAX requests in intervals rather than letting the successful AJAX calls just call themselves, all the while risking severe server load through multiple requests and not just making another call once you had a successful one come back.

在这一点上,我喜欢写的解决方案,其中Ajax调用只是称自己是在完成,是这样的:

In this light, I like to write solutions where the AJAX calls just call themselves on completion, something like:

// set your delay here, 2 seconds as an example...
var my_delay = 2000;

// call your ajax function when the document is ready...
$(function() {
    callAjax();
});

// function that processes your ajax calls...
function callAjax() {
    $.ajax({
        // ajax parameters here...
        // ...
        success: function() {
            setTimeout(callAjax, my_delay);
        }
    });
}

我希望这是有道理的! :)

I hope this makes sense! :)

再次回顾这一点后,它已经引起了我的注意,也有在PHP code。在我需要澄清和原来的问题一个问题的地址。

After reviewing this again, it's been brought to my attention that there was also a problem in the PHP code in the original question that I needed to clarify and address.

虽然上面的脚本将在建立AJAX之间的延迟来电,当在原来的职位加入到PHP code中的脚本将只是工作的伟大回声倒是出多次行的SQL查询选择的数量,使用相同的名称创建多个功能,可能使所有的AJAX调用同时...不是非常酷在所有...

Although the script above will work great in creating a delay between AJAX calls, when added to the PHP code in the original post the script will just be echo'd out as many times as the number of rows the SQL query selects, creating multiple functions with the same name and possibly making all AJAX calls simultaneously...not very cool at all...

考虑到这一点,我提出以下额外的解决方案 - 创建一个阵列与可能由JavaScript一个元素在同一时间内消化PHP脚本来实现期望的结果。首先,PHP构建JavaScript数组字符串...

With that in mind, I propose the following additional solution - create an array with the PHP script that may be digested by the JavaScript one element at a time to achieve the desired result. First, the PHP to build the JavaScript array string...

<?php 
    include("includes/configuratie.php");
    $strSQL = mysql_query("SELECT workerID FROM tWorkers ORDER BY workerID ASC");

    // build the array for the JavaScript, needs to be a string...
    $javascript_array = '[';
    $delimiter = '';
    while($row = mysql_fetch_assoc($strSQL))
    {
        $javascript_array .= $delimiter . '"'. $row['workerID'] .'"'; // with quotes
        $delimiter = ',';
    }
    $javascript_array .= ']';
    // should create an array string, something like:
    // ["1","2","3"]
?>

其次,JavaScript的消化过程中,我们刚刚创建的阵列...

Next, the JavaScript to digest and process the array we just created...

// set your delay here, 2 seconds as an example...
var my_delay = 2000;

// add your JavaScript array here too...
var my_row_ids = <?php echo $javascript_array; ?>;

// call your ajax function when the document is ready...
$(function() {
    callAjax();
});

// function that processes your ajax calls...
function callAjax() {
    // check to see if there are id's remaining...
    if (my_row_ids.length > 0)
    {
        // get the next id, and remove it from the array...
        var next_id = my_row_ids[0];
        my_row_ids.shift();
        $.ajax({
            cache    : false,
            url      : 'ajax2.php',
            data     : "workerID=" + next_id, // next ID here!
            dataType : 'json',
            success  : function(data) {
                           // do necessary things here...
                           // call your AJAX function again, with delay...
                           setTimeout(callAjax, my_delay);
                       }
        });
    }
}