javascript - jQuery:每个循环暂停直到完成

标签 javascript jquery foreach

我有以下 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/

相关文章:

javascript - 显示 2 后在 slider 中显示 1 张图片

javascript - 如何在 PHP 后获取 td 值?

javascript - 创建自动填充 JavaScript 数组

PHP 和 jQuery : Return incremental results as search runs

c# - 结合 foreach 和使用

php foreach,为什么使用数组的引用传递很快?

javascript - "export ' 默认 ' (imported as ' __vue_script__ ') was not found in ' !!babel-loader

javascript - 完整的日历更改图标以在不可编辑的事件上悬停

javascript - 如何制作一个导入 .css 文件的按钮?

reactjs - 如何等待 forEach 循环完成