如果scan_status.php =='完成',有没有人知道是否有办法停止setInterval?
每3秒数据(html)来自scan_status.php。
我想要的是在scan_status.php完成时停止更新数据。
提前致谢!
赖安
jQuery(function($){
setInterval(function(){
$.get( 'dynamic/page_count.php?u=<?php echo $uid; ?>', function(newRowCount){
$('#pagecounter').html( newRowCount );
});
$.get( 'dynamic/individual_count.php?u=<?php echo $uid; ?>', function(newIndRowCount){
$('#individual_counter').html( newIndRowCount );
});
$.get( 'dynamic/total_word_count.php?u=<?php echo $uid; ?>', function(totalWordCount){
$('#total_word_count').html( totalWordCount );
});
$.get( 'dynamic/scan_status.php?u=<?php echo $uid; ?>', function(scanStatus){
$('#scan_status').html( scanStatus );
});
$.get( 'dynamic/download_status.php?u=<?php echo $uid; ?>', function(downloadStatus){
$('#download_status').html( downloadStatus );
});
},3000); // 5000ms == 5 seconds
});
答案 0 :(得分:1)
通过存储调用返回的值来保存对setInterval
处理程序的引用。
var interval = setInterval(function(){...});
如果要清除它,请使用clearInterval( interval );
所以在你的例子中
jQuery(function($){
var interval = setInterval(function(){
$.get( 'dynamic/page_count.php?u=<?php echo $uid; ?>', function(newRowCount){
$('#pagecounter').html( newRowCount );
});
$.get( 'dynamic/individual_count.php?u=<?php echo $uid; ?>', function(newIndRowCount){
$('#individual_counter').html( newIndRowCount );
});
$.get( 'dynamic/total_word_count.php?u=<?php echo $uid; ?>', function(totalWordCount){
$('#total_word_count').html( totalWordCount );
});
$.get( 'dynamic/scan_status.php?u=<?php echo $uid; ?>', function(scanStatus){
$('#scan_status').html( scanStatus );
if (scanStatus == 'finished')
{clearInterval(interval);}
});
$.get( 'dynamic/download_status.php?u=<?php echo $uid; ?>', function(downloadStatus){
$('#download_status').html( downloadStatus );
});
},3000); // 5000ms == 5 seconds
});
答案 1 :(得分:0)
setInterval
会返回一个ID,您可以将其传递给clearInterval
以停止重复的通话。
顺便说一句,setInterval不是jQuery,而是Ye Plain Olde Javascript。
答案 2 :(得分:0)
您必须在拨打clearInterval
时获得的ID上使用setInterval
。像这样:
var intervalId = setInterval(
在scan_status回调上,然后调用
clearInterval(intervalId);