开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜