2017-06-20 23 views
1

現在,我建立一個排隊系統,我有一個像下面的AJAX功能:我婉改變setTimeout的值時,有數據變化

<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秒更新一次表格視圖,如果有數據更改。

我想要的是,當queue_list.php上有數據變化時,setTimeout將變爲20秒(播放語音呼叫),但如果沒有數據變化,setTimeout值將不會改變。 如何更改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> 

回答

0

setTimeout回報timeoutID,你可以用這個ID來取消已設置超時。

但我不建議你在這裏使用超時,試試setInterval。 以下是它的外觀

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); 
} 
3

您可以將超時值聲明爲變量並將其清除。當你需要清除你

ClearTimeout (workerTimeout); 

,然後再次將其設置爲新值

setTimeout(worker, 3000); 

將成爲

var workerTimeout = setTimeout(worker, 3000); 

workerTimeout = setTimeout (worker, 20000); 
+0

我不知道如何使用它,我如何檢查數據是否更新,然後設置新的超時? – Gumilar

0

這很簡單:將超時值分配給變量並在需要時更改超時變量。事情是這樣的:

(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); 
    } 
    }); 
})(); 
0

的setTimeout是一個原生的JavaScript函數,該函數調用一個函數或在指定的延遲後執行代碼(毫秒)

setTimeout(function(){ 

worker() // call your function here 


},3000);