Jump to content

setInterval Help


S Murder

Recommended Posts

checkURL() is called in the constructor.

//Constructor	//Called every half second to see if the url has changed	checkURL:	function()			{				alert("checked URL");				var paramStr = this.getParams();				var URLHashStr = this.getURLHash();				//if there was a change				if(paramStr != URLHashStr)				{					this.onhashchange(params, URLHashStr.toQueryParams);					this.setParams(URLHashStr);				}			}

//Constructor	initialize:	function()			{				//alert("initialized");				//make shift Event Detection.				//checkURL method will be called regularly 				//every half second, appearing to the user				//as an instantaneous event				setInterval("this.checkURL()", 500);			}

When I use setInterval("this.checkURL", 500); the checkURL method is called once, and that's it. But if I use any other syntax ("this.checkURL()", this.checkURL, this.checkURL()) I get a new error every half second saying the method doesn't exist. How do I get this to work?

Link to comment
Share on other sites

Oh, I can't remember exactly how to solve this - aspnetguy knows, I think - but I might be able to help. When you use setInterval and setTimeout, you cannot use "this" to reference the current object.Try this instead:

var that = this;setInterval(function() { that.checkURL(); }, 500);

Maybe aspnetguy (or someone else) will come by and tell me how wrong I am. :)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...