admin管理员组文章数量:1435859
I'm trying to loop 10x over a test (waiting for the condition to be true), but somehow it's not working.
Here is what I have:
// my counter
$.testHelper.countDown = function(test, iteration) {
var ticker = iteration || 0;
if (ticker > 10) {
return false;
}
window.setTimeout(function() {
if (test === true) {
console.log("SENDING");
return true;
}
ticker += 1;
$.testHelper.countDown(test, ticker);
}, 1000);
};
// my test
$.testHelper.testForElement = function(element) {
var result;
console.log($i.find(element).length > 0); // true
result = $.testHelper.countDown(
$i.find(element).length > 0
);
console.log(result); // undefined
return result;
};
My problem is that although my condition equates to true
before I'm calling countdown, the answer from countDown is undefined
. So my logs e in like this:
// true - before firing my countDown method
// undefined - should not log
// SENDING - response from countDown (= true)
Question:
From the code displayed, is there a reason, why my undefined
is logged before countDown
is through and returns true
?
Thanks!
I'm trying to loop 10x over a test (waiting for the condition to be true), but somehow it's not working.
Here is what I have:
// my counter
$.testHelper.countDown = function(test, iteration) {
var ticker = iteration || 0;
if (ticker > 10) {
return false;
}
window.setTimeout(function() {
if (test === true) {
console.log("SENDING");
return true;
}
ticker += 1;
$.testHelper.countDown(test, ticker);
}, 1000);
};
// my test
$.testHelper.testForElement = function(element) {
var result;
console.log($i.find(element).length > 0); // true
result = $.testHelper.countDown(
$i.find(element).length > 0
);
console.log(result); // undefined
return result;
};
My problem is that although my condition equates to true
before I'm calling countdown, the answer from countDown is undefined
. So my logs e in like this:
// true - before firing my countDown method
// undefined - should not log
// SENDING - response from countDown (= true)
Question:
From the code displayed, is there a reason, why my undefined
is logged before countDown
is through and returns true
?
Thanks!
Share Improve this question asked Mar 28, 2014 at 23:06 frequentfrequent 28.6k61 gold badges187 silver badges336 bronze badges1 Answer
Reset to default 6Erm, because setTimeout
is always asynchronous? That's kind of the whole point.
Here's a possibility for you:
function when(condition,then) {
// condition must be a callback that returns `true` when the condition is met
if( condition()) then();
else setTimeout(function() {when(condition,then);},1000);
}
This will poll once every second until the condition is met, and then do whatever is given in the then
callback.
本文标签: jqueryWhy is my synchronous code using setTimeout behaving asynchronous in JavaScriptStack Overflow
版权声明:本文标题:jquery - Why is my synchronous code using setTimeout behaving asynchronous in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745667313a2669362.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论