簡體   English   中英

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

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

我想做的是在do while循環滿足條件后停止setInterval。

我的問題是即使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;
}

你根本不需要while循環。 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) {
        }
    });
}

使用clearInterval函數來停止間隔。

另請注意, 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