Edward Posted January 23, 2009 Share Posted January 23, 2009 Hi, I am building an instant messaging system into a website, and am looping an ajax function to retrieve messages from the datase, as follows: var interval window.onload = function() { chat_event_view(); interval = setInterval('chat_event_view()', 10*1000); // 10 secs between requests }; However, when the user clicks 'submit' to add their message, it also retrieves the messages from the database. What I want to avoid is the chat_event_view() function running within 10 seconds of adding a message. Does anyone know if there is a way to stop the chat_event_view() function when the add() function is executed, and then resume it after 10 seconds? This way "new" messages could be styled differently for 10 seconds when they are first retrieved/read. Thank you. Quote Link to comment Share on other sites More sharing options...
corbin Posted January 24, 2009 Share Posted January 24, 2009 Well, you have the ID of the event trigger, so you could just cancel the event. Psuedo code: onsubmit: clearInterval(IntervalID); //called interval in your script GetPosts(); setInterval('blah', x); You could consider wrapping it in a class as to avoid global variables, but you shouldn't run into any errors as long as no other scripts clash. By the way, setTimeout is more widely supported than setInterval (used to be anyway. maybe setInterval is more widespread now). Quote Link to comment Share on other sites More sharing options...
F1Fan Posted January 24, 2009 Share Posted January 24, 2009 I agree that setTimeout is better. I suggest setting a global variable to true. Then whenever you don't want it to run, set it to false, then back to true when it's done. var stoprunning = false; function yourNormalFunction(){ stoprunning = true; //do whatever stoprunning = false; } function chat_event_view(){ //whatever else you do... setTimeout('chat_event_view()',10000); } Quote Link to comment Share on other sites More sharing options...
corbin Posted January 24, 2009 Share Posted January 24, 2009 Why use a bool to check whether or not to run it? I would personally just schedule/unschedule the task. Quote Link to comment Share on other sites More sharing options...
Edward Posted January 25, 2009 Author Share Posted January 25, 2009 Hi Corbin and F1Fan, Thank you both for your help. I've now got it working like a charm! I decided to stick with SetInterval though, partly because I have already set my functions up in this way, and partly because of the article I read here (http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/) which is in favour of it, which is what I found on Google when I was first trying to find out how to run looped functions. Here is an example of my finished code, in case it helps anyone else. window.onload = function() { chat_event_view(); interval_chat_event_view = setInterval('chat_event_view()', 5000); }; function chat_event_view() { // View messages } function chat_event_add() { clearInterval(interval_chat_event_view); // Add message then view messages interval_chat_event_view = setInterval('chat_event_view()', 5000); } Thanks very much again to you both! Quote Link to comment Share on other sites More sharing options...
corbin Posted January 25, 2009 Share Posted January 25, 2009 No prob ;p. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.