How to implement a timeout in Javascript, not the window.timeout
but something like session timeout
or socket timeout
- basically - a "function timeout
"
A specified period of time that will be allowed to elapse in a system before a specified event is to take place, unless another specified event occurs first; in either case, the period is terminated when either event takes place.
Specifically, I want a javascript observing timer
that will observe the execution time of a function and if reached or going more than a specified time then the observing timer
will stop/notify the executing function.
Any help is greatly appreciated! Thanks a lot.
You could execute the code in a web worker. Then you are still able to handle timeout events while the code is running. As soon as the web worker finishes its job you can cancel the timeout. And as soon as the timeout happens you can terminate the web worker.
I'm not entirely clear what you're asking, but I think that Javascript does not work the way you want so it cannot be done. For example, it cannot be done that a regular function call lasts either until the operation completes or a certain amount of time whichever comes first. That can be implemented outside of javascript and exposed through javascript (as is done with synchronous ajax calls), but can't be done in pure javascript with regular functions.
Unlike other languages, Javascript is single threaded so that while a function is executing a timer will never execute (except for web workers, but they are very, very limited in what they can do). The timer can only execute when the function finishes executing. Thus, you can't even share a progress variable between a synchronous function and a timer so there's no way for a timer to "check on" the progress of a function.
If your code was completely stand-alone (didn't access any of your global variables, didn't call your other functions and didn't access the DOM in anyway), then you could run it in a web-worker (available in newer browsers only) and use a timer in the main thread. When the web-worker code completes, it sends a message to the main thread with it's results. When the main thread receives that message, it stops the timer. If the timer fires before receiving the results, it can kill the web-worker. But, your code would have to live with the restrictions of web-workers.
Soemthing can also be done with asynchronous operations (because they work better with Javascript's single-threaded-ness) like this:
setTimeout()
for your timeout time.clearTimeout()
and proceed.For example, here's how to put a timeout on the loading of an image:
Share a variable between the
observing timer
and theexecuting function
.Implement the
observing timer
withwindow.setTimeout
orwindow.setInterval
. When theobserving timer
executes, it sets an exit value to the shared variable.The
executing function
constantly checks for the variable value.. and returns if the exit value is specified.You can achieve this only using some hardcore tricks. Like for example if you know what kind of variable your function returns (note that EVERY js function returns something, default is
undefined
) you can try something like this: define variableand run test in seperate "thread":
and finally run function:
This only works if you know that your function either does not return any value (i.e. the returned value is
undefined
) or the value it returns is always "not false", i.e. is not converted tofalse
statement (like0
,null
, etc).