How to implement a “function timeout” in Javascript – not just the ‘setTimeout’

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:

  1. Start an asynchronous operation like an ajax call or the loading of an image.
  2. Start a timer using setTimeout() for your timeout time.
  3. If the timer fires before your asynchronous operation completes, then stop the asynchronous operation (using the APIs to cancel it).
  4. If the asynchronous operation completes before the timer fires, then cancel the timer with clearTimeout() and proceed.

For example, here’s how to put a timeout on the loading of an image:

function loadImage(url, maxTime, data, fnSuccess, fnFail) {
    var img = new Image();

    var timer = setTimeout(function() {
        timer = null;
        fnFail(data, url);
    }, maxTime);

    img.onLoad = function() {
        if (timer) {
            fnSuccess(data, img);

    img.onAbort = img.onError = function() {
        fnFail(data, url);
    img.src = url;

Leave a Comment