Javascript 设置JS中的中断流超时?
有人能解释一下为什么这不起作用,我的Javascript中的流似乎被破坏了 经常发生的事情是这样的:“otherFunction”的最后一个if语句的计算结果总是true,但似乎因为我在那里得到了一个超时,所以返回从未发生过 我是否需要通过首先将true从匿名超时函数返回给我的“otherFunction”,然后依次从该函数返回给被调用方来传递返回?不知道怎么做Javascript 设置JS中的中断流超时?,javascript,comparison,equals,Javascript,Comparison,Equals,有人能解释一下为什么这不起作用,我的Javascript中的流似乎被破坏了 经常发生的事情是这样的:“otherFunction”的最后一个if语句的计算结果总是true,但似乎因为我在那里得到了一个超时,所以返回从未发生过 我是否需要通过首先将true从匿名超时函数返回给我的“otherFunction”,然后依次从该函数返回给被调用方来传递返回?不知道怎么做 myObj.ready = function() { var self = this; var readyFlag
myObj.ready = function() {
var self = this;
var readyFlag = self.otherFunction();
if (readyFlag == true) {
$("#slide1").remove();
}
else {
//If not fully transitioned in yet, postpone 1,5s
setTimeout(function(){
self.ready();
},1500);
return false; //end execution here, wait for timeout to pick it back up
}
}
myObj.otherFunction = function() {
//check twice (with a couple of milliseconds inbetwwen) and get the same value, transition must be finished...
var check1 = $("#slide").css('-webkit-transform');
setTimeout(function(){
console.error('check1: '+check1); //print: none
var check2 = $("#slide1").css('-webkit-transform');
console.error('check2: '+check2); //print: none
if (check1 == check2) {
console.error('transition IS ready...');
return true;
}
else {
console.error('transition NOT ready...');
console.error('check2: '+check2);
return false;
}
},30);
}
您不能将数据从“超时”功能返回到
其他功能
。如果我正确理解了您的要求,您似乎运气不好。您无法将数据从“超时”函数返回到其他函数。如果我正确理解了您想要什么,那么您似乎运气不好。myObj.otherFunction
不会返回任何内容。return
语句从内部匿名函数返回,该函数在otherFunction
完成后至少30毫秒才被调用。因此,我不能在这里返回true,我必须对其进行重新构造,以代替通过返回的true继续执行,我必须生成第三个函数,在为true时由setTimeout调用?这两个都是有效选项。异步和同步,了解这意味着什么。如果您想知道第二个函数何时完成,则需要使用回调或触发事件。