-1

i would Like to stop auto refresh when i click on a specfic div this is how my code looks like at the moment

jquery.js

var documentInterval;
$(document).ready(function(){
  documentInterval setInterval(function(){
      $('#show_here').load('fetch_something.php')
  }, 3000);
});

$('#stop_auto_refresh').click(function(){
  clearInterval(documentInterval);
});

index.php

<div id="show_here"></div>
<div id="stop_auto_refresh" onclick="documentInterval()">Click To Stop Auto Refresh</div>

I would like to stop auto refresh when click on the id stop_auto_refresh is this possible ?

James
  • 23
  • 4

2 Answers2

1

Intervals are cleared as using the clearInterval(intervalReference). You can add a click handler using the .click() jQuery api since you are using jQuery.

var documentInterval;

$(document).ready(function() {
   documentInterval = setInterval(function(){
      $('#show_here').load('fetch_something.php')
   }, 3000);

   $('#stop_auto_refresh').click(function(){
     clearInterval(documentInterval) 
   })

});    
Dan
  • 2,830
  • 19
  • 37
0

You need to set your interval function as a var and then you can use an onclick event to fire another function that contains clearInterval(varObject);

martinB0103
  • 245
  • 1
  • 6
  • Look at the example at [W3 Schools](http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_setinterval_clearinterval) – martinB0103 May 09 '16 at 03:59