Javascript - Jquery .load() and setInterval() issue.

Posted by Joe on Stack Overflow See other posts from Stack Overflow or by Joe
Published on 2010-03-23T01:34:22Z Indexed on 2010/03/23 1:41 UTC
Read the original article Hit count: 263

Filed under:
|

Let's say I do this:

$("#content").load(...);

Within what I'm loading some javascript is included:

var myCounter = 0;
var myInterval = setInterval(function(){
   myCounter++;

   $("#counter-display").html("Count: "+myCounter);
});

For an unknown reason, if I reload the content with $("#content").load(...); - myInterval is now being called twice.

I tried doing something like:

if (myInterval !== undefined){
   //dont set interval again
}

However, it doesn't work. Does anyone know any method so that myInterval is cleared on .load, without needing to put the javascript outside of the loaded file?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery