這工作正常,但我試圖找到一個很好的解決方案,當所有響應已通過Ajax從數據庫中提取時,阻止Javascript setInterval
。如何在所有Ajax調用完成後停止setInterval?
For循環內for循環可以工作,我想?!
HTML:
<!-- code is simplefied, problem is not about syntax, but the way to do it. -->
SELECT id FROM database WHERE x=y
$rows = countrows(); //know how much different output there is
for(i=1; i <= rows; i++) // Show div everytime there's a response
{
$data-> fetch();
<div id="<?php echo "div" . $i;?>">
<?php echo 'id' . $data['id']; ?>
</div>
}
比方說,我們從數據庫3層的反應,我們有3個div
s的3角不同的id
秒。
使用Javascript:
setInterval(getResponse(), 1000); //execute function every second
getResponse()
{
for(i=1; i <= $rows; i++)
{
//Here is the problem
//Stop getResponse function execution when all responses has been taken
if(response[i] && reponse[i] && ...)
{
//Stop function
}
//Select id . $data['id'] WHERE div . $i = i;
// GET VARIABLE FROM DATABASE WHERE ID = 'id' . $data['id'];
if(response)
{
//INSERT THIS VARIABLE IN THE RIGHT DIV.
$response[i] = true;
}
}
}
你可以'break;'或're爲了「停止函數中的for循環」,使用「turn'' – 2014-08-29 08:33:10
['window.clearInterval(intervalId)'](https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers.clearInterval ) – BlitZ 2014-08-29 08:33:44
問題是如何知道何時將所有響應[i]設置爲true以便我可以阻止它 – gr3g 2014-08-31 09:42:19