ethicalhack3r
ethicalhack3r

Reputation: 1132

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; ?>', function(downloadStatus){
  $('#download_status').html( downloadStatus );
 });
},3000); // 5000ms == 5 seconds
});

Upvotes: 1

Views: 4173

Answers (3)

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196187

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

Upvotes: 1

Minkiele
Minkiele

Reputation: 1280

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

Upvotes: 0

GolezTrol
GolezTrol

Reputation: 116140

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.

Upvotes: 0

Related Questions