How to terminate Jquery setIntereval?
I am trying to terminate setIntreval that refresh m开发者_开发技巧y page. But cleareIntreval() not work for me.
I have
$chatInterval = setInterval(function(){
$.post('user/home/show_conversation.php',{ f_id : userID},function(ajaxCevap){
$('#chatbox').html(ajaxCevap);
$('#chatbox').scrollTop = $('#chatbox').scrollHeight;
});
},10000);
And when I click button I use clearInterval($chatInterval);
But is says that $chatInterval not defined. yes Those are in different function scope.How can I declare common variable setInterval?
You have a typo there, and make sure the variable you created $chatInterval
is in the scope of the location you call clearInterval()
from.
clearInterval($chatInterval);
If you need to declare the variable in a scope accessible to both, either declare it at the top-level or store it on an element somewhere using the .data()
method of jQuery: http://api.jquery.com/data/
I solved it deleting $ element from
chatInterval = setInterval(function(){
$.post('user/home/show_conversation.php',{ f_id : userID},function(ajaxCevap){
$('#chatbox').html(ajaxCevap);
$('#chatbox').scrollTop = $('#chatbox').scrollHeight;
});
},10000);
Now I can call it in any function.
精彩评论