函數返回的結果我想結果,當函數checkLoading()
是調用true
。等到jQuery的
我有一個jQuery函數:
function checkLoading() {
console.log('checking loading');
if ($('div.vX.UC').css('display') === 'none') {
console.log('Loading Complete');
return true;
} else {
setTimeout(function() {
checkLoading();
}, 500)
}
}
我調用這個函數象下面這樣:
if(checkLoading()){
// do something
}
但上面的函數返回undefined
,而不是等待final
回報。
這個我試過用Deferred
。但這是行不通的,我嘗試了下面的解決方案:
function checkLoading() {
var dfrd1 = $.Deferred();
console.log('checking loading');
if ($('div.vX.UC').css('display') === 'none') {
console.log('Loading Complete');
return dfrd1.promise();
} else {
setTimeout(function() {
checkLoading();
}, 500)
}
// return dfrd1.resolve();
//return false;
}
checkLoading().done(function() {
// do something.
});
但是這個解決方案對我不起作用。
什麼正在改變這種隱藏呢? '$(「div.vX.UC」)。CSS(「顯示」)===「none''這就是你想要返回/回調出的,循環使用超時是不是一個好主意恕我直言 –