最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is it possible to return a value from setTimeout without a callback function? - Stack Overflow

programmeradmin6浏览0评论

Long story short, is it possible to do something like this without a callback function?

function foo(){
  return 'foo';
}
function bar(){
  x = setTimeout(foo, 2000);
  alert(x);
}

Without modifying the foo() function. Adding an intermediary function would be fine, but I don't think that'll acplish anything.

Long story: I'm looking to simulate window.showModalDialog via window.open without having to do significant code rewrites everywhere the dang thing was used. The only suggestions I have found are to use a while loop or ping the server to simulate sleep(). Neither solution strikes me as ideal, and I am wondering if there is any other way to do something like this minus the callback function method?

Long story short, is it possible to do something like this without a callback function?

function foo(){
  return 'foo';
}
function bar(){
  x = setTimeout(foo, 2000);
  alert(x);
}

Without modifying the foo() function. Adding an intermediary function would be fine, but I don't think that'll acplish anything.

Long story: I'm looking to simulate window.showModalDialog via window.open without having to do significant code rewrites everywhere the dang thing was used. The only suggestions I have found are to use a while loop or ping the server to simulate sleep(). Neither solution strikes me as ideal, and I am wondering if there is any other way to do something like this minus the callback function method?

Share Improve this question edited Apr 17, 2015 at 23:56 Christian 2,2001 gold badge19 silver badges35 bronze badges asked Dec 5, 2012 at 19:13 SerhiySerhiy 2,5553 gold badges34 silver badges49 bronze badges 5
  • Would Web workers do what you need? – kojiro Commented Dec 5, 2012 at 19:17
  • 2 The answers aren't entirely accurate and wanted to add one point of clarification. setTimeout does return a value, just not what you expect. It returns the id of the timer, so that you can, for whatever reason, cancel the timeout and prevent it from running. Edit: an edit to this response clarifies things accurately: stackoverflow./a/13730735/1795053 – Eli Gassert Commented Dec 5, 2012 at 19:17
  • no, Web workers won't do it, esp since I'm shooting for IE support back till 7, thanks for the info Eli looked through jAndy's answer, didn't know – Serhiy Commented Dec 5, 2012 at 19:23
  • 1 Remember, setTimeout is not supposed to be used like a pause or delay. The point is that you can set it free to go off and do its thing while continuing on forward. For the code in your example to work, the function would have to stop for 2 seconds between lines before x was ready for the alert(). If you want it to function like a delay, try continuing your program in foo(), or have foo() call another function that continues the program. – Sandy Gifford Commented Dec 5, 2012 at 19:43
  • Sandy, that's exactly what I had wanted, a delay without having foo() call another function. I guess I wanted my cake and to eat it as well :) – Serhiy Commented Dec 5, 2012 at 19:50
Add a ment  | 

4 Answers 4

Reset to default 6

Short answer: No.

I would answer in more detail, but since you asked for a "short" answer and ruled another "callback" out", there is only this left to say.

setTimeout will always return an identifier to clear the timeout and anything you return from the function you pass into setTimeout is getting ignored.

No, that is not possible. Yet, you could return something that represents the future value and lets you add the callback afterwards: a Promise.

Refactor your design. You can more than likely implement what you are looking for with a timeout, but not like this. Don't buck convention, use a callback

function foo(){
 alert('foo');
}
function bar(){
 setTimeout(foo, 2000);
}

As far as saving the timeout in a variable goes, that is used for clearing timeouts.

timeoutID = setTimeout();
window.clearTimeout(timeoutID);

setTimout already returns something, but it's not the value of the evalauated expression. It returns a timout object that lets you abort and do other stuff later if you need to. setTimeout just tells a function to execute in the future.

If you wanted to get a functionality that might work for your circumstance, you could use something like this:

var answer = null;
setTimeout(function() {
    /*do something to 'answer'*/
    answer = 14;
}, 1000);

NOTE: This means that you have little control over when "answer" is set. That is why it's always best to use call backs because you're telling your code "in the future, when you have a value, perform this function on it".

发布评论

评论列表(0)

  1. 暂无评论