Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7034151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:09:05+00:00 2026-05-28T01:09:05+00:00

How to implement a timeout in Javascript, not the window.timeout but something like session

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:09:05+00:00Added an answer on May 28, 2026 at 1:09 am

    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) {
                clearTimeout(timer);
                fnSuccess(data, img);
            }
        }
    
        img.onAbort = img.onError = function() {
            clearTimeout(timer);
            fnFail(data, url);
        }
        img.src = url;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to implement session timeout kind of thing for a JFrame .
Trying to implement a timeout parameter for connecting to a server but I'm not
I'm currently working on a ASP.Net 3.5 project and trying to implement session timeout
How to implement timeout in this code: Response.Write(@<script language='javascript'>alert('some alert');</script>); Response.Redirect(Request.ApplicationPath); I want to
i'm trying to find the best way to implement a timeout with quartz but
How to implement a session timeout feature for an asp.net mvc application? I want
Possible Duplicate: Timeout on a Python function call I want to implement that when
I've written the following function to implement a timeout feature using NetworkStream 's asynchronous
I want to implement a chat application but I don't want to use timeout
is there any way to implement timeout option for individual tests in a bunch

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.