Jump to content

using setInterval to loop, only when user inactive?


Edward

Recommended Posts

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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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);
}

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.