gpt4 book ai didi

javascript - 我想在数据发生变化时更改 setTimeout 值

转载 作者:行者123 更新时间:2023-11-30 11:35:30 26 4
gpt4 key购买 nike

现在,我正在构建一个队列系统,我有如下的 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>

函数中,如果数据有变化,系统会每3秒刷新一次数据,更新表格 View 。

我想要的是,当queue_list.php有数据变化时,setTimeout会变成20秒(播放语音通话),但如果没有数据变化,setTimeout值不会改变。如何更改 setTimeout 值?

队列列表.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>

最佳答案

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

setTimeout(worker, 3000);

会变成

var workerTimeout = setTimeout(worker, 3000);

当你需要清除它的时候你

ClearTimeout (workerTimeout);

然后再次将其设置为新值。

workerTimeout = setTimeout (worker, 20000);

关于javascript - 我想在数据发生变化时更改 setTimeout 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44646742/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com