admin管理员组文章数量:1326326
I have a function that calls some service and returns the response. If the response is FALSE, it waits 1 second to ask the service again (which then probably returns TRUE).
How can I do to call my function "checkService()" once, and get the real value? (the first or second try, decided by the function) I set the RET value inside the function, but the functions always return the first RET, because the setTimeout is asynchronous.
In other words, I need some "sleep" trick, or any solution (may be jQuery too).
function checkService() {
//this may return TRUE or FALSE
var RET = someServiceResponse();
// here waits 1 second, then ask the service again
if( RET == true ) {
return true;
} else {
setTimeout(
function() {
//it must return the second response of the service
RET = someServiceResponse();
},
1000
);
// I want the checkService() return the response after de timeout
return RET;
}
}
function alertResponse() {
alert( checkService() );
}
I have a function that calls some service and returns the response. If the response is FALSE, it waits 1 second to ask the service again (which then probably returns TRUE).
How can I do to call my function "checkService()" once, and get the real value? (the first or second try, decided by the function) I set the RET value inside the function, but the functions always return the first RET, because the setTimeout is asynchronous.
In other words, I need some "sleep" trick, or any solution (may be jQuery too).
function checkService() {
//this may return TRUE or FALSE
var RET = someServiceResponse();
// here waits 1 second, then ask the service again
if( RET == true ) {
return true;
} else {
setTimeout(
function() {
//it must return the second response of the service
RET = someServiceResponse();
},
1000
);
// I want the checkService() return the response after de timeout
return RET;
}
}
function alertResponse() {
alert( checkService() );
}
Share
Improve this question
edited Jul 10, 2015 at 16:54
undefined
6,4614 gold badges52 silver badges59 bronze badges
asked Aug 4, 2014 at 23:26
AberelAberel
1621 gold badge1 silver badge14 bronze badges
3
- 1 Is someServiceResponse getting its result from an ajax call? That may be the heart of your issue. – Travis J Commented Aug 4, 2014 at 23:28
- although the 1 sec wait seems useful, its easily broken if the call takes more time to return. I would remend splitting up this functionality with a callback function that fires when the response is actually delivered. – Bosworth99 Commented Aug 4, 2014 at 23:31
- Travis, no. However, I dont mind if waits takes more than 1 second. It's not important that time, just need to wait some (half second or so) and chek it again – Aberel Commented Aug 5, 2014 at 0:04
2 Answers
Reset to default 3You should use a callback function when you expect a result from the service.
Like this :
function checkService(callback) {
//this may return TRUE or FALSE
var RET = someServiceResponse();
// here waits 1 second, then ask the service again
if( RET == true ) {
callback(RET);
} else {
setTimeout(
function() {
//it must return the second response of the service
RET = someServiceResponse();
callback(RET);
},
1000
);
// I want the checkService() return the response after de timeout
return RET;
}
}
So when you want to call the service, you just need to do :
checkService(function(status){
alert(status);
// Here some code after the webservice response
});
Googling 'javascript setTimeout callback' here's a handy jsFiddle about 3 results down:
getData('http://fakedomain1234./userlist', writeData);
document.getElementById('output').innerHTML += "show this before data ...";
function getData(dataURI, callback) {
// Normally you would actually connect to a server here.
// We're just going to simulate a 3-second delay.
var timer = setTimeout(function () {
var dataArray = [123, 456, 789, 012, 345, 678];
callback(dataArray);
}, 3000);
}
function writeData(myData) {
console.log(myData);
}
http://jsfiddle/cwbuecheler/Y9Ca8/
本文标签: asynchronoushow to make Javascript setTimeout returns value in a functionStack Overflow
版权声明:本文标题:asynchronous - how to make Javascript setTimeout returns value in a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742201893a2432115.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论