【发布时间】:2015-11-12 15:37:24
【问题描述】:
我是 javascript 的初学者,我想弄清楚为什么我的 while 循环实际上不会循环多次,即使条件总是满足。
我有一个发送 API 请求的函数:
var get_status = function(trid, count) {
console.log(count);
var req = {
method: 'GET',
url: 'theUrlHere',
headers: {'headers'}
}
$http(req).success(function(data) {
if (data.transaction_status != 'Pending') {
// do something with the data
console.log('true');
return true;
}
else {
console.log('False');
return false;
}
}).error(function(data) {
// show an error popup
console.log('true');
return true;
})
}
};
我想调用这个函数直到它返回true,所以我这样称呼它:
var count = 0;
while (get_status(id, count) === false) {
count += 1;
}
count 变量只是添加来查看它循环了多少次,即使控制台中显示“False”,它也保持为 0。
我在这里误解了一些行为吗?
编辑我明白为什么这不起作用。我的意图是只要交易状态处于待处理状态,就显示 iframe。我想过不断发送请求,直到交易状态不是“待处理”,但我知道还有更优化的方法。
【问题讨论】:
标签: javascript angularjs