gpt4 book ai didi

javascript - 类似于 jQuery when/then : deferred execution with break

转载 作者:行者123 更新时间:2023-11-28 21:07:32 25 4
gpt4 key购买 nike

我正在寻找一种能够运行不同功能的解决方案,但其中一些功能需要超时,并且所有后续功能都需要等待前一个功能完成。每个函数都应该能够打破完整的流程。

现在我想将所有函数插入堆栈并循环它们:

function foo() {
// bar() should wait as long the following is finished:
setTimeout(function(){
if ((new Date()).getSeconds() % 2) {
alert('foo');
// break loop through functions (bar is not called)
}
else {
// start next function (bar is called)
}
}, 1000);
}
function bar() {
setTimeout(function(){
alert('bar')
}, 1000);
}
var functions = new Array('foo', 'bar');
for (var i = 0, length = functions.length; i < length; i++) {
window[functions[i]]();
}

但是如何包含等待/中断?!

注意:这应该适用于 2 个以上的函数(函数的数量是可变的)

注2:我不想使用 jQuery。

最佳答案

注意:我已经更新了我的答案,请参阅帖子底部。

好吧,我们来看看。

您正在使用 window[func]() 方法,因此您应该能够存储和使用每个函数的返回值。

Proof :

function a(){
return "value";
}

var ret_val = window['a']();
alert(ret_val);

让我们创建一个返回规则:
如果函数返回true,则继续执行流程。
如果函数返回false,则中断执行流程。

function a(){
//Do stuff
return (condition);
}

for(i = 0; i < n; i++){
var bReturn = window['function']();
if(!bReturn) break;
}

现在让我们付诸实践。

function a(){
//Do stuff
return ((new Date()).getSeconds() % 2); //Continue?
}

function b(){
//Do stuff
return true; //Continue?
}

function c(){
//Do stuff
return false; //Continue?
}

function d(){
//Do stuff
return true; //Continue?
}

var functions = new Array('a', 'b', 'c', 'd');

for (var i = 0; i < functions.length; i++ ) {
var bReturn = window[functions[i]]();
if(!bReturn) break;
}

根据您执行脚本的时间,例如均匀或不均匀的时间段,它只会执行函数a或执行函数a b & c。在每个功能之间,您可以处理您的正常业务。
当然,条件可能因您的案例中的每个单独功能而异。

这是一个JSFiddle example您可以在其中看到它的实际效果。

<小时/>

通过一些小的修改,例如,您可以使其如果函数 a 返回 false,它将跳过以下函数并继续执行下一个或之后的函数。

改变

for (var i = 0; i < functions.length; i++ ) {
var bReturn = window[functions[i]]();
if(!bReturn) break;
}

至此

for (var i = 0; i < functions.length; i++ ) {
var bReturn = window[functions[i]]();
if(!bReturn) i++;
}

每次函数返回 false 时,都会使其跳过一个函数。

你可以试试here .

<小时/>

顺便说一句,如果您正在寻找“暂停”脚本的等待函数,您可以使用 this piece of code .

function pausecomp(millis){
var date = new Date();
var curDate = null;

do {
curDate = new Date();
}while(curDate-date < millis);
}
<小时/>

更新

调整代码后,现在可以与 setTimeout 配合使用。

这个想法是,你有一个入口点,从数组中的第一个函数开始,并传递当前在数组中位置的索引参数,然后将索引增加一以执行下一个函数。

Example |代码

function next_function(index){
if(index >= functions.length) return false;
setTimeout(function(){
window[functions[index+1]](index+1);
}, 1000);
}

function a(index){
//Do stuff
if(((new Date()).getSeconds() % 2)) return false; //Stop?
next_function(index);
}

function b(index){
//Do stuff
if(false) return false; //Stop?
next_function(index);
}

function c(index){
//Do stuff
if(true) return false; //Stop?
next_function(index);
}

function d(index){
//Do stuff
if(false) return false; //Stop?
next_function(index);
}

var functions = new Array('a', 'b', 'c', 'd');

//entry point
window[functions[0]](0);

关于javascript - 类似于 jQuery when/then : deferred execution with break,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9517832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com