IE8 中的 setTimeOut 和无响应脚本出现问题

Issue with setTimeOut and non-response script in IE8

本文关键字:脚本 问题 响应 中的 setTimeOut IE8      更新时间:2023-09-26

我遇到了大量处理的问题,导致IE8中的脚本无响应错误(不,我无法让用户使用更好的浏览器(。

然后我读到应该可以拆分任务并在验证的不同部分之间将控制权交还给浏览器。 因此,我决定根据我找到的一些代码制作一个简单的示例,以找出突破点在哪里。 真正的代码正在做大量的jquery验证引擎处理。

我尝试使用jsFiddle

,但我无法让jsFiddle在IE8中运行。 无赖。 所以,我必须在这里内联分享。

当我第一次加载它时,它似乎工作得很好。 我按下按钮,两个功能都没有问题。 但是,后续推送会导致脚本无响应错误。 我已经尝试了模拟工作函数中的循环数量。超过 125 万个循环,它因脚本无响应而死亡。

对onClick的单独调用不应该重新启动无响应的计数器吗? 我在这里错过了什么?

<html>
<head>
<script>
var progress = null;
var goButton = null;
window.onload = function() {
    progress = document.getElementById("progress");
    goButton = document.getElementById("goButton");
}
function runLongScript(){
    // clear status
    progress.value = "";
    goButton.disabled=true;
    var tasks = [function1, function2];
    multistep(tasks,null,function() {goButton.disabled=false;});
}
function function1() {
    var result = 0;
    var i = 1250000;
    for (;i>0; i--) {
        result = result + 1;
    }
    progress.value = progress.value + "f1 end ";
}
function function2() {
    var result = 0;
    var i = 1250000;
    for (;i>0; i--) {
        result = result + 1;
    }
    progress.value = progress.value + "f2 end";
}
function multistep(tasks, args, callback){
    var tasksClone = tasks.slice(0); //clone the array
    setTimeout(function(){
        //execute the next task
        var task = tasksClone.shift();
        task.apply(null, args || []);
        //determine if there's more
        if (tasksClone.length > 0){
            setTimeout(function () {
               multistep(tasksClone, args, callback);
            }, 100);
        } else {
            callback();
        }
    }, 100);
}
</script>
</head>
<body>
    <p><input type="button" id="goButton" onClick="runLongScript();" value="Run Long Script" /></p>
    <input type="text" id="progress" />
</body>
</html>

当按钮已被按下时,您永远不会调用clearTimeout()来删除当前正在运行的按钮。 在启动另一个setTimeout之前添加一个 if 语句,并检查一个语句是否已运行,如果正在运行,则清除它,然后继续。 如果您有任何疑问,这里有一个链接可以帮助您:https://developer.mozilla.org/en-US/docs/Web/API/window.clearTimeout