Ercan
Ercan

Reputation: 2773

How to terminate Jquery setIntereval?

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?

Upvotes: 0

Views: 246

Answers (2)

Ercan
Ercan

Reputation: 2773

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.

Upvotes: 0

Orbling
Orbling

Reputation: 20612

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/

Upvotes: 1

Related Questions