?
Solved

Timer and Javascript Function Query

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

timer ( )
{
    myfun();


}



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.

Thanks.

0
Comment
Question by:Vigus
  • 2
  • 2
5 Comments
 

Expert Comment

by:sijohnkj
ID: 26077905
timer()
{
myfun(int a,int b,int x)
{
x=a+b;
print x:
}
}
it may help u..
0
 

Author Comment

by:Vigus
ID: 26077936
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.
0
 
LVL 5

Accepted Solution

by:
siddagrl earned 2000 total points
ID: 26078053
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.

<html>
<body>
<script language="javascript">

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

function sleep(delay)
{
   alert('start');

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

   alert('end');
}

</script>

<form>
    <input type=button value="Start Timer" onClick="timer()">
</form>
</body>
</html>
0
 
LVL 5

Assisted Solution

by:siddagrl
siddagrl earned 2000 total points
ID: 26078148
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);
}
-------------
0
 

Author Closing Comment

by:Vigus
ID: 31667653
thnx
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Dependencies in Software Design In software development, the idea of dependencies (http://en.wikipedia.org/wiki/Coupling_%28computer_programming%29) is an issue of some importance. This article seeks to explain what dependencies are and where they …
Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
The goal of the tutorial is to teach the user how to how to record live broadcast.
Suggested Courses

850 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