Chris Bier
Chris Bier

Reputation: 14447

Jquery set interval problem

I am making a facebook-like chat system so the users of my system can chat with eachother. For some reason, when I run this interval to check for new chats, it works fine in Firefox but not in Internet Explorer. Is there anything specific about setInterval that Internet Explorer does not like?

Edit: The specific problem I am facing in IE, is that instead of loading up the new chats via this setInterval, nothing is happening at all. It works fine in firefox and loads the new chats as they are entered into the database. I am not sure which part of this code might be causing the error but I have a suspicion that it is the setInterval function.

Or could the be the frequency of Ajax requests?

  setInterval(function(){ 
      // Load Chats
     },500);

On a side Note: On every interval I am having an ajax request load a PHP file that query's the database to see if there are any new chats added to the database since the page load. If it does then it sets up the new chat window for that chat session. Is it wrong to keep querying the database and running mulitpule ajax requests like that? Is there a better way?

This is a simplified version of what I am doing:

setInterval(function(){
   // Get new DB Entries 
   $.get(url, data,handlerFunction); 

   function handlerFunction(result){
   // Load new chats if they exist in the DB 
   } 
 },500);

Upvotes: 0

Views: 3797

Answers (2)

Amr Elgarhy
Amr Elgarhy

Reputation: 68912

One thing I can tell for now, that in cases as yours "ajax calls with timers" I avoid using setInterval and I use setTimeout instead.

Because for example int=self.setInterval("clock()",1000); This will call the clock function Immediately and will call it every one second regardless the excution time clock() may take inside.

On the other side, var t=setTimeout("clock()",1000); This will wait one second then will start calling the clock() function and if you called settimeout again it will wait till the clock() finish execution and will call it again.

Some useful links about this:
http://www.w3schools.com/jsref/met_win_setinterval.asp
http://www.w3schools.com/js/js_timing.asp
http://zetafleet.com/blog/why-i-consider-setinterval-harmful
http://javascript.about.com/library/blstvsi.htm

Upvotes: 1

Quentin
Quentin

Reputation: 943527

Is there anything specific about setInterval that Internet Explorer does not like?

No, there isn't.

Or could the be the frequency of Ajax requests?

Yes. Have the response handler set up the next request, don't send off another one every half second no matter how long they take to come back. You'll just hit the limit of simultaneous requests and queue up a long stack of them.

Upvotes: 3

Related Questions