1

The Section() is responsible to look on database and get any HTML code if some things are matched. If nothing matched, there is no callback (but I can callback an empty thing).

Then it calls addIt() and display the HTML. Works fine.

My question is when there is some HTML to get from database, to stop the timer? Because for now it adds HTML every 10 seconds.

function addIt(data) { $(data).insertAfter('#hello'); }

setInterval(function Section(){
      $.getJSON("domain"+ number, addIt);
}, 10000);
sergdenisov
  • 8,327
  • 9
  • 48
  • 63
EnexoOnoma
  • 8,454
  • 18
  • 94
  • 179
  • [Possible duplicate](https://stackoverflow.com/questions/2901108/how-do-i-clear-this-setinterval) – Drops May 19 '15 at 08:36
  • use clearInterval(); http://www.w3schools.com/jsref/met_win_clearinterval.asp – yugi May 19 '15 at 08:36

3 Answers3

1

Maybe something like this :

var x = setInterval(function Section(){
  $.getJSON("domain"+ number, addIt);
}, 10000);

if( // something ) {
   clearInterval(x);
 }
AshBringer
  • 2,614
  • 2
  • 20
  • 42
0
var timerId;

function addIt(data) { 
  $(data).insertAfter('#hello');
}

function section() {
  $.getJSON().done(function(data) {
    addIt(data);

    if (timerId) {
      clearInterval(timerId);
    }
  });
}

timerId = setInterval(section, 10000);
Miraage
  • 3,334
  • 3
  • 26
  • 43
0

I would never call AJAX in an interval. Instead use the callback to call again

function Section(){
   $.getJSON("domain"+number,addIt);
}
function addIt(data) { 
    if (data) $(data).insertAfter('#hello'); 
    else setTimeout(Section,10000); // call again in 10 seconds unless data was returned
}
Section(); // first call

If the call results in an ERROR when not returning data, try this:

$.getJSON("domain"+number, function(data) {
  if (data) $(data).insertAfter('#hello'); 
})
.fail(function() { 
  setTimeout(Section,10000);
});
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • hello, thank you for your answer. I am a newbie in AJAX, jQuery and these, what I am trying is to repeat the `Section()` every x seconds. What is another way to do this, a better way ? – EnexoOnoma May 19 '15 at 08:44
  • Like I showed you. It will call Section (after a successful call) in 10 seconds but only if no data. If data, it will stop. My method will make sure the loop stops on error too. – mplungjan May 19 '15 at 08:45
  • I see, this is nice. Can you please tell me why, meaning in a theoretical level why is it bad ? – EnexoOnoma May 19 '15 at 08:46
  • If your server is slower that the timeout seconds to respond, you will call it again and again while it is still processing – mplungjan May 19 '15 at 08:47
  • Meaning that if I set the timer to 1 sec, but for a reason it took 2 seconds to respond, that means it will run 2 times. But with your case, it will run upon completion. right? – EnexoOnoma May 19 '15 at 08:48
  • Yes exactly. It will run x seconds after successful (empty) return – mplungjan May 19 '15 at 08:50
  • I really like the logic, but it seems that the timer isn't working. if it does not returns something, the timer doesnt run again – EnexoOnoma May 19 '15 at 09:28
  • There was a spelling mistake in the setTimeout - please check that. Also check that data is null or empty. If it is not null, you need to change `if (data)` to for example `if (data.result=="")` – mplungjan May 19 '15 at 09:42
  • When nothing is returned, rhrough the console inspect element, i get "this request has no response data available". However, if there is data, I get the data – EnexoOnoma May 19 '15 at 09:47