如何从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);
}
...

最好将此resultId传递到其他函数中,该函数将设置计时器,然后您将能够使用直接值。

在'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);
    }
    });