簡體   English   中英

如何從setinterval函數返回值

[英]how to return a value from setinterval function

如何從setinterval函數返回值?

$.ajax({
        type : "POST",
        url:"<?php echo TESTMINE_APP_URL; ?>/ajax/export-details",
        data:'paginationHash='+paginationHash+'&exportType='+exportType+'&userId='+userId,
        dataType: "html",
        success: function(requestId) 
        {
            var myVar = setInterval(function(){checkStatusOfRequest(requestId)},9000);
            alert(myVar);
        }
        });

您不能使用setInterval做到這一點。 實現所需目標的更好方法是使用您希望在延遲函數中返回的值。 就像是,

...
success: function (requestId) {
    setInterval(function () {
        var myVar = checkStatusOfRequest(requestId);
        alert(myVar);
    }, 9000);
}
...

您不會在“ myVar”變量中獲得“ checkStatusOfReques”的返回值

使用以下代碼

    $.ajax({
    type : "POST",
    url:"<?php echo TESTMINE_APP_URL; ?>/ajax/export-details",
    data:'paginationHash='+paginationHash+'&exportType='+exportType+'&userId='+userId,
    dataType: "html",
    success: function(requestId) 
    {
        var myVar = setInterval(function(){

            var ReturnValue = checkStatusOfRequest(requestId)
             // This is your function return value
              alert(ReturnValue);

        },9000);
    }
    });

最好在其他函數中傳遞此resultId,這將設置計時器,然后您將可以使用直接值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM