Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

timer problem 2

Status
Not open for further replies.

lwfg

Programmer
Feb 9, 2009
52
US
Trying to make a timer. This runs onload but gets browser error something like script might take to much time and gives choice to continue or not.

Got a "recursive" one working but have been told to avoid recursion.

What am I doing wrong ?

<script>
function a()
{
var m1,m2=new Date()
,t1=m1.getTime()
,t2=t1;
while (t2-t1<3000)
t2=m2.getTime();
alert('while over')
}
</script>
 
Hi

There is nothing wrong with recursion. Your theory is completely wrong.

Take a look at the [tt]window[/tt] object's [tt]setTimeout()[/tt] / [tt]clearTimeout()[/tt] or [tt]setInterval()[/tt] / [tt]clearInterval()[/tt] methods.
JavaScript:
[b]function[/b] [COLOR=darkgoldenrod]a[/color][teal]()[/teal]
[teal]{[/teal]
  [COLOR=darkgoldenrod]setTimeout[/color][teal]([/teal][b]function[/b][teal]()[/teal] [teal]{[/teal] [COLOR=darkgoldenrod]alert[/color][teal]([/teal][green][i]'no while over'[/i][/green][teal])[/teal] [teal]}[/teal][teal],[/teal][purple]3000[/purple][teal])[/teal]
[teal]}[/teal]

Feherke.
 
am in no way a javascript expert but...
* I don't see anything recursive in the code you show.
* I do see however a busy (while) loop.
perhaps you should look at setInterval(..., ...) and/or setTimeout(..., ...)
 
hah. got called to a meeting before I was able to post.
should have refreshed the page when I got back.
 
Have another non recursive way that looks like will work.If it doesn't, I'll be back asking questions.

Feherke, This is one of the articles that scared me away from my recursive way of using a timer. What do you think?
The recursive one I got to work would be running about 8 hours a day.

JustinEzequiel, the recusive one I got working is another one,sorry I wasn't clear.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top