Take the 2-minute tour ×

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);

Without modifying the foo() function. Adding an intermediary function would be fine, but I don't think that'll accomplish 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've Googled to are, to use a while loop or to ping the server to simulate sleep(). Neither solution strikes me as ideal and am wondering if there is any other way to do something like this minus the callback function method of course?

share|improve this question
Would Web workers do what you need? –  kojiro Dec 5 '12 at 19:17
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.com/a/13730735/1795053 –  Eli Gassert Dec 5 '12 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 Dec 5 '12 at 19:23
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 Dec 5 '12 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 Dec 5 '12 at 19:50
add comment

3 Answers

up vote 5 down vote accepted

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.

share|improve this answer
Thanks for the details, I wasn't really asking for a short answer, I meant I was giving a short / long scenario of my problem. However I appreciate the short answer, and the details. Thank you. –  Serhiy Dec 5 '12 at 20:17
add comment

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

share|improve this answer
hmm, thank you for that, looks interesting this may help me solve my problem, will explore and report further –  Serhiy Dec 5 '12 at 19:22
hmm, no not quite, still requires a call back to populate the data and modification to the foo() function which is what I was trying to avoid, thanks for the tip though, maybe more useful in other places, I was hoping for some form of a blocking the way alert, confirm, and showModalDialog do –  Serhiy Dec 5 '12 at 19:32
No, JavaScript is single-threaded, and does not allow blocking. It's simply impossible. –  Bergi Dec 5 '12 at 19:40
yeah, that's what I feared, sucks that they let those 3 things block and nothing else, I've found while based hacks, but pinging the CPU while I have a window.open going is not an ideal solution either... –  Serhiy Dec 5 '12 at 19:42
add comment

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(){
function bar(){
 setTimeout(foo, 2000);

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

timeoutID = setTimeout();
share|improve this answer
add comment

Your Answer


