我有一个for循环,我正在循环。
我想制作一个自定义模态并在继续之前等待响应。
我怎样才能做到这一点?我知道我要等待回电。
就像这个例子:
for(var x in array){
alert(x);
console.log(x);
}
它完全符合我的要求。但我想要三个按钮。 但警报不是javascript的一部分(?它在浏览器中。)
那么,你们有个主意吗?
我正在考虑做这样的事情:
var run = true;
function foo(){
if (run){
setTimeout(foo, 500);
}
}
function stop(){
run = false;
}
foo();
然后等待停止,在继续之前调用按钮单击。但这是非常好的做法吗?
或者使用lambda函数作为customAlert的参数和一个“全局”变量,该变量保存我正在经历的数组的当前位置并使用函数执行此操作。 Like:检查数组是否仍然保持大于X的键。 然后再次执行该功能,每次都增加全局X。
谢谢你输入代码: 哦,我有个主意;我只是在匿名函数中使用lostsource的解决方案,所以我不会得到全局变量。优异。
(function(){
})();
答案 0 :(得分:77)
假设这是你的数组
var list = ['one','two','three'];
您可以尝试使用此循环/回调方法
var x = 0;
var loopArray = function(arr) {
customAlert(arr[x],function(){
// set x to next item
x++;
// any more items in array? continue loop
if(x < arr.length) {
loopArray(arr);
}
});
}
function customAlert(msg,callback) {
// code to show your custom alert
// in this case its just a console log
console.log(msg);
// do callback when ready
callback();
}
用法:
// start 'loop'
loopArray(list);
JSFiddle:http://jsfiddle.net/D9AXp/
答案 1 :(得分:3)
MaggiQall,我知道你有答案,但我有一个灵活的解决方案,你或者对其他人可能感兴趣。
解决方案依赖于jQuery(1.7+)和jQuery UI的dialog
,但是作为Array原型的自定义方法实现,而不是作为jQuery插件实现。
这是Array方法:
Array.prototype.runDialogSequence = function(dialogCallback, startIndex, endIndex){
startIndex = Math.max(0, startIndex || 0);
endIndex = Math.min(this.length - 1, endIndex || this.length - 1);
var sequenceIndex = 0,
arr = this,
dfrd = $.Deferred().resolve(startIndex);
function makeCloseFn(seqData){
return function(event, ui){
if(seqData.continue_) { seqData.dfrd.resolve(seqData.arrayIndex+1, seqData); } //continue dialog sequence
else { seqData.dfrd.reject(seqData.arrayIndex, seqData); } //break dialog sequence
}
}
$.each(this, function(i){
if(i < startIndex || i > endIndex) { return true; }//continue
dfrd = dfrd.then(function(arrayIndex){
var seqData = {
dfrd: $.Deferred(),
arrayIndex: arrayIndex,
sequenceIndex: ++sequenceIndex,
length: 1 + endIndex - startIndex,
item: arr[arrayIndex],
continue_: false
};
dialogCallback(seqData).on("dialogclose", makeCloseFn(seqData));
return seqData.dfrd.promise();
});
});
return dfrd.promise();
}
Array.runDialogSequence()
依赖于:
这是一个示例“openDialog”函数,带有解释性注释:
function openDialog(seqData){
/*
seqData is an object with the following properties:
dfrd: A Deferred object associated with the current dialog. Normally resolved by Array.runDialogSequence() to run through the sequence or rejected to break it, but can be resolved/rejected here to force the dialog sequence to continue/break. If resolved, then pass (seqData.arrayIndex+1, seqData), or custom values. If rejected, typically pass (seqData.arrayIndex, seqData).
arrayIndex: The current array index.
sequenceIndex: The current index within the sequence. Normally the same as arrayIndex but Differs when a non-zero startIndex is specified.
length: The full length of the dialog sequence.
item: The current item from the array.
continue_: (false) Set to true to allow the sequence to continue.
*/
var item = seqData.item;
var $d = $("#d");
$d.dialog({
title: 'Dialog (' + seqData.sequenceIndex + ' of ' + seqData.length + ')',
modal: true,
buttons: {
"Continue": function(){
seqData.continue_ = true;//set to true before closing to go to next dialog.
$(this).dialog("close");
},
"Cancel": function(){
$(this).dialog('close');//closing with seqData.continue_ set to its default value false will break the dialog sequence.
}
}
}).find("#message").text(item);//Typically you will do a lot more here to populate the dialog with item data.
return $d;//openDialog() must return a dialog container, jQuery-wrapped.
}
Array.runDialogSequence()
返回一个jQuery promise
,允许在对话框序列完成时执行自定义操作(完成功能)或在中间序列中执行自定义操作(失败功能)。
以下是几个示例调用:
//Simplest possible
$("#myButton1").click(function(){
myArray.runDialogSequence(openDialog);
});
//Call with custom startIndex and endIndex, and chanined `.then()` to provide custom actions on break/completion.
$("#myButton2").click(function(){
myArray.runDialogSequence(openDialog, 1, 3).then(function(i, seqData){
alert('All dialogs complete - last item = "' + seqData.item + '"');
}, function(i, seqData){
alert('Dialog sequence was broken at item ' + i + ' "' + seqData.item + '"');
});
});
<强> DEMO 强>
这与我的想象所允许的接近一般化解决方案一样。