Javascript 我想在数据更改时更改setTimeout值

Javascript 我想在数据更改时更改setTimeout值,javascript,php,ajax,Javascript,Php,Ajax,现在,我正在构建一个队列系统,我有如下ajax功能: <script type="text/javascript" > (function worker() { $.ajax({ url: 'http://localhost/queue/queue_list.php', success: function(data) { $('#result').html(data); }, complete: function() {

现在,我正在构建一个队列系统,我有如下ajax功能:

<script type="text/javascript" >
(function worker() {
  $.ajax({
    url: 'http://localhost/queue/queue_list.php', 
    success: function(data) {
      $('#result').html(data);
    },
    complete: function() {
      // Schedule the next request when the current one's complete
     setTimeout(worker, 3000);
    }
  });
})();
<script>
<div id="result"></div>

(职能工作人员(){
$.ajax({
网址:'http://localhost/queue/queue_list.php', 
成功:功能(数据){
$('#result').html(数据);
},
完成:函数(){
//在当前请求完成时安排下一个请求
setTimeout(工人,3000);
}
});
})();
在该功能中,如果数据发生更改,系统将每3秒刷新一次数据,以更新表视图

我想要的是,当queue_list.php上有数据更改时,setTimeout将更改为20秒(播放语音通话),但如果没有数据更改,setTimeout值将不会更改。 如何更改设置超时值

queue_list.php

<?php
function nomornya($loket,$huruf){
include"database.php";
    $sqlnyah="select count(id) as ceknya from panggilan_antrian where loket='$loket' and huruf='$huruf'";
    $sqlnyah2="select nomor from panggilan_antrian where loket='$loket' and huruf='$huruf'";
    $querynyah=mysqli_query($sqlnyah);
    $angkan=mysqli_fetch_assoc($querynyah);
    $angkany=$angkan[ceknya];
    if($angkany!=0){
        $querynyah2=mysqli_query($sqlnyah2);
        $angkan2=mysqli_fetch_assoc($querynyah2);
        $angkanya=$angkan2[nomor];
    }else{
        $angkanya="0";
    }
    echo $angkanya;

}
?>
<style>
table,tr,td{
    font-size:1.7em;
}
</style>
<table style="width:100%;text-align:center;">
    <tr style="background:#000000; color:#ffffff;">
        <td>COUNTER</td>
        <td>NOMOR</td>
    </tr>
    <tr style="background:#b1c2f3">
        <td>1</td>
        <td><?php nomornya(1,null); ?></td>
    </tr>
    <tr style="background:#d2f3b1">
        <td>2A</td>
        <td><?php nomornya(2,A); ?></td>
    </tr>
    <tr style="background:#b1c2f3">
        <td>2B</td>
        <td><?php nomornya(2,B); ?></td>
    </tr>
    <tr style="background:#d2f3b1">
        <td>2C</td>
        <td><?php nomornya(2,C); ?></td>
    </tr>
    <tr style="background:#b1c2f3">
        <td>3</td>
        <td><?php nomornya(3,null); ?></td>
    </tr>
    <tr style="background:#d2f3b1">
        <td>4</td>
        <td><?php nomornya(4,null); ?></td>
    </tr>
</table>
返回timeoutID,您可以使用此ID取消已设置的超时

但我不建议您在这里使用超时,请尝试设置间隔。 下面是它的外观

var updateBlocked = false;
function worker() {
    if (updateBlocked) return;
    // your code to fetch new info
}

setInterval(worker, 3000);

function onSomeEvent() {
    updateBlocked = true;
    setTimeout(function(){updateBlocked = false;}, 20000);
}

您可以将超时声明为变量并清除它

setTimeout(worker, 3000);
将成为

var workerTimeout = setTimeout(worker, 3000);
当你需要清理它的时候

ClearTimeout (workerTimeout);
然后再次将其设置为新值

workerTimeout = setTimeout (worker, 20000);

很简单:将超时值分配给变量,并在需要时更改超时变量。大概是这样的:

(function worker() {
  $.ajax({
    url: 'http://localhost/queue/queue_list.php', 
    success: function(data) {
      $('#result').html(data);
    },
    complete: function() {
      var timeout = 3000;
      //change in data, since timeout was already set on previous data change. Hence, update timeout value too and clear previous timeout
      if(typeof timer!='undefined') {
       clearTimeout(timer);
       timeout = 20*1000;
      }
      // Schedule the next request when the current one's complete
     var timer = setTimeout(worker, timeout);
    }
  });
})();

setTimeout是一个本机JavaScript函数,它在指定的延迟(毫秒)后调用函数或执行代码


我不知道如何使用它,如何检查数据是否更新,然后设置新的超时?
setTimeout(function(){

worker() // call your function here


},3000);