我有以下 jQuery 循环,但在每个循环操作中我都有用户交互,代码应该等到用户交互完成。是否有可能暂停循环或是否有其他可能实现它?
jQuery.each([345, 897, 345 /* ... */], function(index, value) {
// User interaction, need to wait the user finishing/answer
// Here is a jQuery UI dialog with an input field in it
// After the user entered their value and hit the submit button
// which fires an own callback which could also continue the loop somehow?
});
最佳答案
您需要放弃 each
并自行处理。一种选择是这样的:
var curIndex = 0;
var ids = [345, 897, 345 /* ... */];
function doNext(){
// check curIndex here to make sure you haven't completed the list, etc.
var id = ids[curIndex++];
// do stuff with this id
}
// THIS IS SOME SORT OF CODE EXECUTED WHEN THE "USER INTERACTION" FINISHES
function interactionDone(){
doNext();
}
关于javascript - jQuery:每个循环暂停直到完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10404209/