简体   繁体   English

如何在jquery中的do while循环中停止setInterval

[英]How to stop setInterval in a do while loop in jquery

I want to do is stop setInterval after the do while loop meet the condition. 我想做的是在do while循环满足条件后停止setInterval。

My problem is even the while loop condition is meet the setInterval is still running. 我的问题是即使while循环条件满足setInterval仍在运行。

do
{
setInterval(
Vinformation();
,500);
}while($('#emailCodeResult').val() !='')

function Vinformation(){
   var data = {};
        data.emailCodeResult = $('#emailCodeResult').val();

    $.ajax({
        type: "POST",
        url: "Oppa.php",
        data: data,
        cache: false,
        dataType:"JSON",
        success: function (result) {



        }
     });
            return false;
}

You don't need while loop here at all. 你根本不需要while循环。 In combination with setInterval it doesn't make sense. setInterval结合使用没有意义。 What you need is probably just setInterval : 你需要的可能只是setInterval

var interval = setInterval(Vinformation, 500);

function Vinformation() {

    if ($('#emailCodeResult').val() == '') {
        clearInterval(interval);
        return;
    }

    var data = {};
    data.emailCodeResult = $('#emailCodeResult').val();

    $.ajax({
        type: "POST",
        url: "Oppa.php",
        data: data,
        cache: false,
        dataType: "JSON",
        success: function (result) {
        }
    });
}

Use clearInterval function to stop interval. 使用clearInterval函数来停止间隔。

Also note, that setInterval expects function reference as the first argument so this setInterval(Vinformation(), 500) is not correct, because you immediately invoke the Vinformation function. 另请注意, setInterval需要函数引用作为第一个参数,因此setInterval(Vinformation(), 500)不正确,因为您立即调用Vinformation函数。

    var itvl1= window.setInterval(function(){
        Vinformation();
    },500);

    function Vinformation(){
        var data = {};
        data.emailCodeResult = $('#emailCodeResult').val();

        if(data.emailCodeResult  !=''){
            window.clearInterval(itvl1);
        };

        $.ajax({
            type: "POST",
            url: "Oppa.php",
            data: data,
            cache: false,
            dataType:"Jenter code hereSON",
            success: function (result) {

            }
        });
        return false;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM