【发布时间】:2014-03-28 23:06:31
【问题描述】:
我正在尝试在测试中循环 10 倍(等待条件为真),但不知何故它不起作用。
这是我所拥有的:
// 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;
};
我的问题是,虽然在我调用倒计时之前我的条件等于true,但countDown 的答案是undefined。所以我的日志是这样进来的:
// true - before firing my countDown method
// undefined - should not log
// SENDING - response from countDown (= true)
问题:
从显示的代码来看,为什么我的undefined在countDown通过并返回true之前被记录是有原因的吗?
谢谢!
【问题讨论】:
标签: javascript jquery jquery-mobile timeout qunit