如何清除JavaScript中的设置超时?

时间:2013-12-10 09:14:29

标签: javascript

function checkStatusOfRequest(requestId) {
    var filePath = "";
    $.ajax({
        type: "POST",
        url: "<?php echo TESTMINE_APP_URL; ?>/ajax/check-export-status",
        data: 'requestId=' + requestId,
        dataType: "json",
        success: function (data) {
            if (data.exportType == 'csv') {
                filePath = $("#csvFilePath").val();

            } else if (data.exportType == 'pdf') {
                filePath = $("#pdfFilePath").val();
            }

            if (data.status == 'downloadReady') {

                fileName = data.fileName;
                $("#statusDisplay").css("visibility", "hidden");
                $("#download").css("visibility", "visible");
                $('#requestId').val(requestId);
                setTimeout(checkStatusOfRequest, 9000);


            }

        }
    });

3 个答案:

答案 0 :(得分:1)

//keep the returned timeoutID 
var timeoutID =   setTimeout(checkStatusOfRequest, 9000);
....
//clear the timeoutID 
clearTimeout(timeoutID );

答案 1 :(得分:0)

您应该先将计时器设置为变量

var statusTimer = setTimeout(checkStatusOfRequest, 9000);

清除定时器呼叫

clearTimeout(statusTimer);

答案 2 :(得分:0)

setTimeout()在指定的毫秒数后调用函数或计算表达式

var myVar = setTimeout(function(){alert("Hi")},1000);

setTimeout()返回的ID值用作clearTimeout()方法的参数。

clearTimeout(myVar);