Solved

Jaavscript Timer Help

Posted on 2008-09-29
6
228 Views
Last Modified: 2012-06-21
Hello Experts,

Below is a javascript countdown. Now what I am trying to do is to call the function after a certain process. In the attach code snippets I just put a an alert then after the countdown timer should be called again. How can I do this.  If you have a better approach is solving my problem that will be cool.

TIA,
gladxml


<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<script> 

<!-- 

// 

 var milisec=0 

 var seconds=30

// var myCounter = document.getElementById('d2'); 

// myCounter.value='60' 

function display(){

var myCounter = document.getElementById('d2');

myCounter.value = '30'

 if (milisec<=0){ 

    milisec=9 

    seconds-=1 

 } 

 if (seconds<=-1){ 

    milisec=0 

    seconds+=1 

 } 

 else 

 {

    milisec-=1 

    myCounter.value=seconds+"."+milisec 

    setTimeout("display()",100) 

 }

 if (myCounter.value == '0.0')

 {

	alert('call the function again');

	display();

 }

} 

-->  

</script> 

<title>Untitled Document</title>

</head>
 

<body>

<form name="MyForm" action="response_normal.asp" method="post">

	<input type="text" size="8" name="d2" id="d2"><br />

</form>

<div id="MyResult"></div>

<script>

display();

</script>
 

</body>

</html>

Open in new window

0
Comment
Question by:gladxml
  • 4
  • 2
6 Comments
 
LVL 15

Author Comment

by:gladxml
ID: 22602505
To make it clearer
I want to call the countdown timer function again after a certain process. SO that the process will just loop again and again. In my example after the alert message the timer function must be triggered.

TIA,
gladxml
0
 
LVL 1

Expert Comment

by:Pronca
ID: 22602945

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<script> 

<!-- 

// 

 var milisec=0 

 var seconds=30

// var myCounter = document.getElementById('d2'); 

// myCounter.value='60' 

function display(){

var myCounter = document.getElementById('d2');

myCounter.value = '30'

 if (milisec<=0){ 

    milisec=9 

    seconds-=1 

 } 

 if (seconds<=-1){ 

    milisec=0 

    seconds+=30+1 

 } 

 else 

 {

    milisec-=1 

    myCounter.value=seconds+"."+milisec 

    setTimeout("display()",100) 

 }

 if (myCounter.value == '0.0')

 {

        alert('call the function again');

        setTimeout("display()",0)

 }

} 

-->  

</script> 

<title>Untitled Document</title>

</head>

 

<body>

<form name="MyForm" action="response_normal.asp" method="post">

        <input type="text" size="8" name="d2" id="d2"><br />

</form>

<div id="MyResult"></div>

<script>

display();

</script>

 

</body>

</html>

Open in new window

0
 
LVL 1

Accepted Solution

by:
Pronca earned 500 total points
ID: 22602958
To clarify my post:

You have to reset the number of seconds to 30+1 (because it will decrement due to milisec == 0). Then you can call display() again. I put display() inside another setTimeout() but it doesn't need to be. All you need to do is reset the seconds.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 15

Author Comment

by:gladxml
ID: 22603145
Ok thanks....

What I did is like this

        alert('call the function again');
        milisec=0 ;
        seconds=30;
        display();

Just need to know if there is are other ways to create a timer function.  Do you know any?

0
 
LVL 15

Author Comment

by:gladxml
ID: 22603171
I have notice when I run the code on the second or third call of he function the timer run faster than the usual. Do you have any idea why it is like that?
0
 
LVL 15

Author Closing Comment

by:gladxml
ID: 31501442
Thanks anyway. Not sure how to fix the timer speed on the next loop. Just need to find a new timer function
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

867 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

17 Experts available now in Live!

Get 1:1 Help Now