使用 settimeout 来防止“停止运行此脚本”在jquery中

发布于 2024-10-19 06:58:33 字数 587 浏览 3 评论 0原文

我有一个很长的 for 循环来验证表中一次一行的文本框(超过 1000 行):我不断收到“停止运行此脚本”错误。很多网站都提到了setTimeout()函数。但我仍然无法让它工作。这是我的代码:

var numRows = $("#tablex").attr('rows').length;
var errorMsg="";
for (var tbRow = 1; tbRow < numRows - 1; tbRow++) {
        var aRecord = $("#tablex tr:eq(" + tbRow + ") td:eq(1)").attr("innerText");
        var curECD = $("#tablex tr:eq(" + tbRow + ") td:eq(7)").find(':text').val().trim();

        if (curECD != "" && isDate(curECD) == false)
            errorMsg += "Date for " + aRecord + " is invalid<br/>";
 }

有人可以帮忙吗?谢谢!!!

I have a long for loop to validate textboxes one row at a time in a table(over 1000 rows): i keep getting the "stop running this script" error. Many websites mentioned setTimeout() function. But i still cant get it to work. Here is my code:

var numRows = $("#tablex").attr('rows').length;
var errorMsg="";
for (var tbRow = 1; tbRow < numRows - 1; tbRow++) {
        var aRecord = $("#tablex tr:eq(" + tbRow + ") td:eq(1)").attr("innerText");
        var curECD = $("#tablex tr:eq(" + tbRow + ") td:eq(7)").find(':text').val().trim();

        if (curECD != "" && isDate(curECD) == false)
            errorMsg += "Date for " + aRecord + " is invalid<br/>";
 }

can anyone help? thanks!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情深缘浅 2024-10-26 06:58:33

我认为上面建议的第一个解决方案更好,但我认为您也可以像这样递归地执行。取决于您的要求:

var errorMsg="";    

function validateRow(row, callback){
    if(row.size() == 0){
        callback();
    }

    var aRecord = $("td:eq(1)", row).attr("innerText");
    var curECD = $("td:eq(7)", row).find(':text').val().trim();

    if (curECD != "" && isDate(curECD) == false)
       errorMsg += "Date for " + aRecord + " is invalid<br/>"; 

    setTimeout(function(){
        validateRow(row.next(), callback);
    }, 25);
}

validateRow($("#tablex tr:first"), function(){
     alert("finished validating");   
});

这将调用 validateRow 并传入第一行。 ValidateRow 将在 setTimeout 上调用自身并传入下一行。当它到达末尾时,它将调用您提供的回调函数。

I think the first suggested above solution is better, but you could also do it recursively like this I think. Depends what your requirements are:

var errorMsg="";    

function validateRow(row, callback){
    if(row.size() == 0){
        callback();
    }

    var aRecord = $("td:eq(1)", row).attr("innerText");
    var curECD = $("td:eq(7)", row).find(':text').val().trim();

    if (curECD != "" && isDate(curECD) == false)
       errorMsg += "Date for " + aRecord + " is invalid<br/>"; 

    setTimeout(function(){
        validateRow(row.next(), callback);
    }, 25);
}

validateRow($("#tablex tr:first"), function(){
     alert("finished validating");   
});

This will call validateRow and pass in the first row. ValidateRow will than call itself on a setTimeout and pass in the next row. When it reaches the end it will call a callback function that you supply.

浅唱々樱花落 2024-10-26 06:58:33

我制作了这个这个小例子来帮助您使用 setTimeout< 枚举块中的列表/代码>。

function enumerate(list, groupSize, callback, complete, debug){
    var iterations = 0;
    for (var i = 0; i < list.length; i+=groupSize, iterations++){
        (function (group, index, isLast){ // new context
            setTimeout(function(){
                for (var j=0; j < group.length;j++){
                    debug && console.log(group[j]);
                    callback.call(group[j], (index+j+1));
                }
                isLast && complete.call();
            }, 1);
        })(list.slice(i, i+groupSize), i, (list.length < i+groupSize)); 
    }
    debug && console.log('iterations: ' + iterations);
}

像这样称呼它:

var trs = $('#tablex tr'),
    errorMsg = '',
    onItem = function(){
        var aRecord = $("td:eq(1)", this).attr("innerText");
        var curECD = $("td:eq(7)", this).find(':text').val().trim();

        if (curECD != "" && isDate(curECD) == false)
           errorMsg += "Date for " + aRecord + " is invalid<br/>"; 
    },
    complete = function(){
        console.log(errorMsg);
    };

enumerate(trs, 5, onItem, complete);

I made this this little example to help you with enumerating a list in chunks with a setTimeout.

function enumerate(list, groupSize, callback, complete, debug){
    var iterations = 0;
    for (var i = 0; i < list.length; i+=groupSize, iterations++){
        (function (group, index, isLast){ // new context
            setTimeout(function(){
                for (var j=0; j < group.length;j++){
                    debug && console.log(group[j]);
                    callback.call(group[j], (index+j+1));
                }
                isLast && complete.call();
            }, 1);
        })(list.slice(i, i+groupSize), i, (list.length < i+groupSize)); 
    }
    debug && console.log('iterations: ' + iterations);
}

Call it like this:

var trs = $('#tablex tr'),
    errorMsg = '',
    onItem = function(){
        var aRecord = $("td:eq(1)", this).attr("innerText");
        var curECD = $("td:eq(7)", this).find(':text').val().trim();

        if (curECD != "" && isDate(curECD) == false)
           errorMsg += "Date for " + aRecord + " is invalid<br/>"; 
    },
    complete = function(){
        console.log(errorMsg);
    };

enumerate(trs, 5, onItem, complete);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文