Javascript jQuery:每个循环暂停直到完成
我有下面的jQuery循环,但是在每个循环操作中我有用户交互,代码应该等到用户交互完成。是否有可能暂停循环或以其他方式实现循环Javascript jQuery:每个循环暂停直到完成,javascript,jquery,foreach,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
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?
});
您需要放弃每个,自己处理。一种选择是这样的:
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();
}
您需要放弃每个,自己处理。一种选择是这样的:
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是单线程的,您可以在每个循环中执行的唯一用户操作是
警报
或确认
。如果这些不能满足您的需求,您需要自己处理每个循环。例如:
//assume foo = [345, 897, 345 /* ... */]
var i = 0;
function handleNextFoo() {
i++;
if(i < foo.length) {
//Do something with foo[i]
//now we wait for use action to call the callbackFromUserAction()
}
}
function callbackFromUserAction() {
//Handle user action
handleNextFoo();
}
//假设foo=[345897345/*…*/]
var i=0;
函数handleNextFoo(){
i++;
如果(i
免责声明:应该为您的产品处理命名约定,以确定变量的作用域并使其更可用。由于javascript是单线程的,您可以在每个循环中执行的唯一用户操作是发出
警报
或确认
。如果这些不能满足您的需求,您需要自己处理每个循环。例如:
//assume foo = [345, 897, 345 /* ... */]
var i = 0;
function handleNextFoo() {
i++;
if(i < foo.length) {
//Do something with foo[i]
//now we wait for use action to call the callbackFromUserAction()
}
}
function callbackFromUserAction() {
//Handle user action
handleNextFoo();
}
//假设foo=[345897345/*…*/]
var i=0;
函数handleNextFoo(){
i++;
如果(i
免责声明:应该为您的产品处理命名约定,以确定变量的作用域并使其更可用。只需为自己构建一个快速迭代器对象,以便轻松处理数组
var iterator = function(array) {
this.array = array;
this.len = array.length;
this.index = 0;
return this;
};
iterator.prototype.next = function(callback) {
callback.call(null, this.array[this.index]);
this.index++;
if (this.index == this.len) this.onend.call(null, this.array);
return this;
};
iterator.prototype.onend = function() {
alert('done');
};
var iterate = new iterator(arr);
iterate.next(function(val) {
console.log(val);
});
这是一个演示
但是,正如dmos指出的,只有alert/confirm可以停止循环,因为它们是javascript用户操作,所以以自己的方式迭代是唯一的方法。只需为自己构建一个快速迭代器对象,以便轻松处理数组
var iterator = function(array) {
this.array = array;
this.len = array.length;
this.index = 0;
return this;
};
iterator.prototype.next = function(callback) {
callback.call(null, this.array[this.index]);
this.index++;
if (this.index == this.len) this.onend.call(null, this.array);
return this;
};
iterator.prototype.onend = function() {
alert('done');
};
var iterate = new iterator(arr);
iterate.next(function(val) {
console.log(val);
});
这是一个演示
但是,正如dmos指出的那样,只有alert/confirm可以停止循环,因为它们是javascript用户操作,所以迭代您自己的方式是唯一的方法。我假设您正在使用循环,因为您不知道要预期多少元素?@Johannes。为什么你要假设它???因为如果你知道有多少元素,你可以不用循环就编码它。当然,这很烦人,但是你不能在没有任何其他干预的情况下停止一个循环。我认为你使用这个循环是因为你不知道要预测多少元素?@Johannes。为什么你要假设它???因为如果你知道有多少元素,你可以不用循环就编码它。当然,这很烦人,但是你不能在没有任何其他干预的情况下停止循环。你能详细说明一下为什么单线程会成为问题,为什么“警报”和“确认”是免疫的吗?谢谢。你能详细解释一下为什么单线程会是一个问题,为什么“警报”和“确认”是免疫的吗?谢谢