clearInterval内的setInterval,无法突破循环,使用jQuery和。员额() [英] clearInterval inside setInterval, unable to break loop, with jquery and .post()

查看:204
本文介绍了clearInterval内的setInterval,无法突破循环,使用jQuery和。员额()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调用clearInterval setInterval函数,这是做一些Ajax取里面,没有任何的运气。

I'm trying to call clearInterval inside setInterval function, which is doing some ajax fetching, without any luck.

var nre = setInterval('checkit()',5000);
$(function() {
    checkit = function(){
        $.post("check.php", { login: "<?php echo $_SESSION['login'];?>" }, function( data ) {
            if (data == 1) {
                $('#debug').html(data);
                window.clearInterval(nre);
            }

        });
    }
});

的一点是,该环路不会断裂,虽然临危正数据这样做。

The point is, that the loop wont break, although recieves positive data to do so.

我读过setInterval函数的异步操作可能是这里的问题。有没有更好的办法来解决呢?

I've read that async action of setInterval function might be the issue here. Is there a better way to solve it?

推荐答案

将所有的相同的范围内,并且不使用的字符串形式的setInterval

Move everything inside the same scope, and don't use the string form of setInterval:

$(function() {
    var nre = setInterval(checkit, 5000);
    function checkit() {
        $.post("check.php", { login: "..." }, function( data ) {
            if (data === 1) {
                $('#debug').html(data);
                clearInterval(nre);
            }
        });
    }
});

这篇关于clearInterval内的setInterval,无法突破循环,使用jQuery和。员额()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆