How to terminate Jquery setIntereval?
-
28-09-2019 - |
Question
I am trying to terminate setIntreval that refresh my 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?
Solution 2
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.
OTHER TIPS
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/