使用 settimeout 来防止“停止运行此脚本”在jquery中
我有一个很长的 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 技术交流群。

发布评论
评论(2)
浅唱々樱花落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);
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我认为上面建议的第一个解决方案更好,但我认为您也可以像这样递归地执行。取决于您的要求:
这将调用 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:
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.