开发者

Stop jquery setInterval when get() == 'finished'?

Does anyone know if there is any way to stop setInterval if scan_status.php == 'finished'?

Every 3 seconds data (html) is brought from scan_status.php.

What I want is to stop updating the data if the scan_status.php is finished.

Thanks in advance!

Ryan

 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; ?&g开发者_如何学Pythont;', function(downloadStatus){
  $('#download_status').html( downloadStatus );
 });
},3000); // 5000ms == 5 seconds
});


Save a reference to the setInterval handler by storing the returned value from the call.

var interval = setInterval(function(){...});

and when you want to clear it use clearInterval( interval );

So in your example

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


setInterval returns an id, that you can pass to clearInterval to stop the repeated calls.

setInterval is not jQuery by the way, but Ye Plain Olde Javascript.


You have to use clearInterval on the id you get when you call setInterval. Like this:

var intervalId = setInterval(

On the scan_status callback then you call

clearInterval(intervalId);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜