Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-20-2013, 12:47 AM   PM User | #1
muldaria
New to the CF scene

 
Join Date: Jan 2013
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
muldaria is an unknown quantity at this point
Question can i get some help with a loop?

I need to loop this timer. can i get some help with that?
Code:
<script> 
<!-- 
// 
 var milisec=0 
 var seconds=60 
 document.counter.d2.value='30' 

function display(){ 
 if (milisec<=0){ 
    milisec=9 
    seconds-=1 
 } 
 if (seconds<=-1){ 
    milisec=0 
    seconds+=1 
 } 
 else 
    milisec-=1 
    document.counter.d2.value=seconds+"."+milisec 
    setTimeout("display()",100) 
} 
display() 
--> 
</script>
muldaria is offline   Reply With Quote
Old 01-20-2013, 02:40 AM   PM User | #2
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,799
Thanks: 30
Thanked 463 Times in 457 Posts
jmrker will become famous soon enough
Lightbulb

Other than what looks to be an older script, what is it doing or not doing to your satisfaction?

Note: It is recommended to use the ';' at the end of each line.
Not mandatory, but will help keep things clear in the future.

Also note the addition of some braces " { } " to clarify the if...else part of the logic.

Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title> Untitled </title>
</head>
<body>
<form name="counter" action="#" method="post" onsubmit="return false">
<input type="text" name="d2" value="">
</form>

<script> 
 var milisec=0; 
 var seconds=60; 
// document.counter.d2.value='30'; // can't see purpose when seconds set to 60 ???

function display(){ 
  if (milisec<=0){ milisec=9; seconds-=1; } 
  if (seconds<=-1){ milisec=0; seconds+=1; } else { milisec-=1; } 
  document.counter.d2.value=seconds+"."+milisec; 
  setTimeout("display()",100); 
} 
display() 
</script>

</body>
</html>
jmrker is offline   Reply With Quote
Users who have thanked jmrker for this post:
muldaria (01-20-2013)
Old 01-20-2013, 02:51 AM   PM User | #3
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 374
Thanks: 3
Thanked 44 Times in 44 Posts
Airblader can only hope to improve
Slightly Off-Topic: Standard JavaScript timers are horribly inaccurate – see the very left example here. The timer on the right is much more accurate, an explanation can be found here.
Airblader is offline   Reply With Quote
Users who have thanked Airblader for this post:
muldaria (01-20-2013)
Old 01-20-2013, 03:12 AM   PM User | #4
muldaria
New to the CF scene

 
Join Date: Jan 2013
Posts: 2
Thanks: 2
Thanked 0 Times in 0 Posts
muldaria is an unknown quantity at this point
Im an intern for a website startup and my "boss" asked me to make a time that goes to 60 seconds infinitely. so can you somehow show me the code for that?
muldaria is offline   Reply With Quote
Old 01-20-2013, 03:18 AM   PM User | #5
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 976
Thanks: 0
Thanked 203 Times in 198 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
If by "loop" you mean execute each iteration of a loop at delayed intervals, then that isn't possible in JavaScript, nor should it be.

That's why setTimeout and setInterval exist and your type of implementation must be used.

Of course it could be improved, not least by ensuring that the function ceases to be called after it is no longer required.
Logic Ali is offline   Reply With Quote
Old 01-20-2013, 04:21 AM   PM User | #6
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 374
Thanks: 3
Thanked 44 Times in 44 Posts
Airblader can only hope to improve
Going completely off-topic now:

When I started as an intern, the first thing they gave me was the book "Clean Code" – and at that point I already had ten years of programming experience. If you're going for making this your profession you really wanna read similar books ... even if you have to do it in your free time. The sooner you learn how to write quality code the easier it will be to take it in.

As for your task: What exactly is your task? A timer that goes from 0 to 60 or from 60 to 0? Does it have to have tenths of seconds? Should it just repeat itself when it reached the end?
Airblader is offline   Reply With Quote
Old 01-20-2013, 06:18 AM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,064 Times in 4,033 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
And a little on the side.

Keeping in mind Airblader's warning about timer accuracy, at least you can simplify the code.

There is no reason for separate variables for seconds and tenths-of-seconds.

Oh, and your use of document.counter.d2.value would indicate you are using a named <form>, which is considered obsolete. So assuming you have a <form> with, properly, an id such as <form id="counter">:
Code:
var ticker = 0;
var countField = document.getElementById("counter").d2;
function tick( )
{
    if ( (ticker-=0.1) <= 0 ) { ticker = 60; }
    countField.value = ticker.toFixed(1);
}
tick( );
setInterval( tick, 100 );
That counts DOWN from 60 to 0.1 before resetting to 60.

If you wanted to go UP, from 0 to 59.9 before resetting to 0:
Code:
var ticker = 60;
var countField = document.getElementById("counter").d2;
function tick( )
{
    if ( (ticker+=0.1) >= 60 ) { ticker = 0; }
    countField.value = ticker.toFixed(1);
}
tick( );
setInterval( tick, 100 );
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:33 PM.


Advertisement
Log in to turn off these ads.