Timer and Javascript Function Query

Posted on 2009-12-17
Last Modified: 2013-11-11
The following is a routine ( i am using it in javascript)

timer ( )


function myfun()
    x = a + b // Replace it with a very very complex calculation, that takes processor time = 5 ms
   print x  ;

now say my timer ( expires in 5ms ) is superfast, and gets called in   1 ms . But my function myfun(), takes 5 ms .
What would happen in this case ? Note: My timer expires in 5ms.


Question by:Vigus

    Expert Comment

    myfun(int a,int b,int x)
    print x:
    it may help u..

    Author Comment

    I am not finding any solution here.
    My question is about, timer's behaviour, when a function taking a considerable time to give output.

    If the function which is getting called itself takes 5ms to solve.
    And a timer is installed , that calls that function after every 1ms. And expires after 5ms.

    then , what would happen. How many times the function would be called ? More than 1 time ? Or only 1 time, because by the time, timer expired, the function couldnot solve itself completely.
    LVL 5

    Accepted Solution

    The browser will queue all the calls after timeouts. I have taken an example of 5 & 1 seconds (instead of milliseconds)

    e.g. Paste the following code in a HTML file. click the button 2/3 times quickly (to set multiple timeouts).
    You will see get either 4 (if you clicked 2 times) or 6 (if clicked 3 times) alerts, where time difference between 'start' & 'end' will be 5 seconds.

    <script language="javascript">

    function timer()
        setTimeout('sleep(5000)', 1000);

    function sleep(delay)

       var start = new Date().getTime();
       while (new Date().getTime() < start + delay);



        <input type=button value="Start Timer" onClick="timer()">
    LVL 5

    Assisted Solution

    Also instead of clicking multiple times quickly a for loop could be used as follows that will work with single click!
    function timer()
       for (i = 0; i < 3; i++)
           setTimeout('sleep(5000)', 1000);

    Author Closing Comment


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Dependencies in Software Design In software development, the idea of dependencies ( is an issue of some importance. This article seeks to explain what dependencies are and where they …
    "Disruption" is the most feared word for C-level executives these days. They agonize over their industry being disturbed by another player - most likely by startups.
    The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
    The goal of the tutorial is to teach the user how to how to record live broadcast.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now