javascript - clearTimeout on a function that has a paramenter -



javascript - clearTimeout on a function that has a paramenter -

i writing chat application can have many chat windows open @ once. every time window opened phone call setinterval on function, update_chat(), updates individual chat window. pass chat_id update_chat()

setinterval("update_chat("+chat_id+")",4000);

chat_id id of chat. can have function update_chat running multiple times on different intervals depending on how many chats open. start chat works fine.

my main question how can stop interval above. don't want stop intervals, 1 associated particular chat. tried this

clearinterval("update_chat("+chat_id+")");

but didn't anything.

i tried

var chat_intervals=[]

chat_intervals[chat_id]=setinterval("update_chat("+chat_id+")",4000);

clearinterval(chat_intervals[end_id]);

it didn't stop interval

clearinterval(docs) takes interval id parameter know interval clear. setinterval (docs) returns interval id when called, store in var , pass clearinterval when want 1 clear.

//start interval, store id var interval_id = setinterval( function () { /* something*/ }, 1000); //clear interval clearinterval(interval_id);

note settimeout (docs) , cleartimeout (docs) work each other in same way.

also note while setinterval can take string argument function run, can take actual functions (which highly preferred). code improve written as:

var update_chat_interval = setinterval(function () { update_chat(chat_id); }, 4000); // clear later: clearinterval(update_chat_interval);

javascript setinterval clearinterval

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -