Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Stop using setInterval, use setTimeout instead (<a href="https://stackoverflow.com/questions/16449312/how-do-i-execute-a-piece-of-code-no-more-than-every-x-minutes/16449886#16449886">How do I execute a piece of code no more than every X minutes?</a>):</p> <pre><code>function generateChart(data, location) { var element = $('#'+location); var chart = new Highcharts.Chart({ // blah blah blah }, foo); var foo = function() { if(element){ // I'm doing stuff every minute setTimeout(foo, 6000); } }; } </code></pre> <p>To stop it, just avoid the setTimeout or make <code>element = null</code>.</p> <p>Maybe my code is a little bit wrong (I'm getting sleep right now), but the thing is to use setTimeout and closures.</p> <p>If inside foo, something longs more than 6 seconds you will be in troubles since setTimeinterval will call it again, please watch <a href="http://www.youtube.com/watch?feature=player_detailpage&amp;v=i_qE1iAmjFg#t=462s" rel="nofollow noreferrer">http://www.youtube.com/watch?feature=player_detailpage&amp;v=i_qE1iAmjFg#t=462s</a> , so, this way you ensure that this will run 6 seconds after the last completed stuff.</p> <p>I'll let this example here to posterity:</p> <p><a href="http://jsfiddle.net/coma/vECyv/2/" rel="nofollow noreferrer">http://jsfiddle.net/coma/vECyv/2/</a></p> <pre><code>var closure = function(id) { var n = 0; var go = true; $('#' + id).one('click', function(event) { go = false; }); var foo = function() { if(go) { console.log(id, n++); setTimeout(foo, 1000); } }; foo(); }; closure('a'); closure('b'); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload