Muq*_*ito 33 javascript jquery
我有一个for循环,我正在循环.
我想制作一个自定义模态并在继续之前等待响应.
我怎样才能做到这一点?我知道我要等待回电.
像这个例子:
for(var x in array){
alert(x);
console.log(x);
}
Run Code Online (Sandbox Code Playgroud)
它完全符合我的要求.但我想要三个按钮.但警报不是javascript的一部分(?它在浏览器中.)
所以,你们有个主意吗?
我在考虑做这样的事情:
var run = true;
function foo(){
if (run){
setTimeout(foo, 500);
}
}
function stop(){
run = false;
}
foo();
Run Code Online (Sandbox Code Playgroud)
然后等待停止,在继续之前调用按钮单击.但这是非常好的做法吗?
或者使用lambda函数作为customAlert的参数和一个"全局"变量,该变量保存我正在经历的数组的当前位置并使用函数执行此操作.像:检查数组是否仍然保持大于X的键.然后再次执行该功能,每次增加全局X.
谢谢你输错代码:哦,我有个主意; 我只是在匿名函数中使用lostsource的解决方案,所以我不会得到全局变量.优秀.
(function(){
})();
Run Code Online (Sandbox Code Playgroud)
los*_*rce 80
假设这是你的阵列
var list = ['one','two','three'];
Run Code Online (Sandbox Code Playgroud)
您可以尝试使用此循环/回调方法
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();
}
Run Code Online (Sandbox Code Playgroud)
用法:
// start 'loop'
loopArray(list);
Run Code Online (Sandbox Code Playgroud)
JSFiddle:http://jsfiddle.net/D9AXp/
| 归档时间: |
|
| 查看次数: |
50906 次 |
| 最近记录: |