[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How can I create a timer that can be displayed in my form?

Posted on 2011-04-29
10
Medium Priority
?
273 Views
Last Modified: 2012-05-11
Hello guys

I'd like to have a timer that could be displayed in my form, this timer will be from 1 minute to 0

I will use this to show the user how many minutes it has before the page be reload.

is there any way to do this in Javascript or Jquery?

thanks
0
Comment
Question by:hidrau
  • 5
  • 3
  • 2
10 Comments
 
LVL 1

Author Comment

by:hidrau
ID: 35490774
Hi, I did this code:


but I am not being able to start the code
<script type="application/javascript">
  var c=60;
  var t;
  var time_is_on=0;
				
  function contagem()
  {
    document.getElementById("textfield").value=c;
    c=c-1;
    t=setTimeout("contagem()",1000);			
  }
		
  function start_time()
  {
    if (!time_is_on)
     {
       time_is_on=1;
       contagem();
     }
   }
		
  start_time();
  setTimeout("location.href='index.asp?f=S'", 1 * 60 * 1000);
</script>

Open in new window

0
 
LVL 9

Assisted Solution

by:Roman Gherman
Roman Gherman earned 800 total points
ID: 35490916
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
	<title>Test</title>
	<script type="text/javascript">
  var c=60;
  var t;
  var time_is_on=0;
  				
  function contagem()
  {
    document.getElementById("textfield").value=c;
    c=c-1;
    t=setTimeout("contagem()",1000);			
  }
		
  function start_time()
  {
	
    if (!time_is_on)
     {
       time_is_on=1;
       contagem();
     }
   }
		
		function startCount()
		{
	start_time();
	setTimeout("location.href='index.asp?f=S'", 1 * 60 * 1000);
  }
</script>
</head>
<body onload="startCount()">
	


            <input type="text" id="textfield"/>

    
</body>
</html>

Open in new window

0
 
LVL 83

Accepted Solution

by:
leakim971 earned 1200 total points
ID: 35490920
Check if the following help :
<html><head>
<script type="application/javascript">
	var secondsLeft = 60; 
	window.onload = setInterval("updateTimer()", 1000);
	function updateTimer() {
		var h = document.createElement("h3");
		h.innerHTML = secondsLeft--;
		document.getElementById("timer").innerHTML = ""; 
		document.getElementById("timer").appendChild(h); 
	}
</script>
</head><body>
<div id="timer" style="position:absolute;top:160px;left:160px"><h3></h3></div>
</body></html>

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 83

Assisted Solution

by:leakim971
leakim971 earned 1200 total points
ID: 35490997
With the redirection :
<html><head>
<script type="application/javascript">
	var secondsLeft = 60; 
	window.onload = setInterval("updateTimer()", 1000);
	function updateTimer() {
		var h = document.createElement("h3");
		h.innerHTML = secondsLeft--;
		if(secondsLeft<0) {
			location.href = "http://www.experts-exchange.com/Programming/Languages/Scripting/JScript/Q_26985174.html";
		}
		else {
			document.getElementById("timer").innerHTML = ""; 
			document.getElementById("timer").appendChild(h); 
		}
	}
</script>
</head><body>
<div id="timer" style="position:absolute;top:160px;left:160px"><h3></h3></div>
</body></html>

Open in new window

0
 
LVL 1

Author Closing Comment

by:hidrau
ID: 35491147
thanks very much
0
 
LVL 1

Author Comment

by:hidrau
ID: 35491174
just one thing,

if I have 420 seconds that is 7 minutes, how can I format my seconds in minutes and seconds lapsed?

0
 
LVL 83

Expert Comment

by:leakim971
ID: 35491220
For example :
var secondsLeft = 420; 
	window.onload = setInterval("updateTimer()", 1000);
	function updateTimer() {
		var h = document.createElement("h3");
                var s = secondsLeft%60;
                var m = parseInt(secondsLeft/60);
		h.innerHTML = m + "min" + s + "s";
		if(secondsLeft--<0) {
			location.href = "http://www.experts-exchange.com/Programming/Languages/Scripting/JScript/Q_26985174.html";
		}
		else {
			document.getElementById("timer").innerHTML = ""; 
			document.getElementById("timer").appendChild(h); 
		}
	}

Open in new window

0
 
LVL 83

Expert Comment

by:leakim971
ID: 35491223
Thanks for the points!
0
 
LVL 9

Expert Comment

by:Roman Gherman
ID: 35491229
nice - my solution is the assisted one?
sorry but @leakim971 and your's solution is not working because of:  <script type="application/javascript"> - which is not correct.



here is a solution with minutes: http://javascript.internet.com/time-date/countdown-timer-2.html
0
 
LVL 83

Expert Comment

by:leakim971
ID: 35491256
@roma1123 is right on IE it don't work, just remove the type attribute or replace it with : text/javascript
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Most folks would know the basics of how Dropbox works, so that’s not the purpose of this article. Security is what it’s all about, so here I’ll share how I choose to secure my Dropbox Account and the Data it contains.
Last month Marc Laliberte, WatchGuard’s Senior Threat Analyst, contributed reviewed the three major email authentication anti-phishing technology standards: SPF, DKIM, and DMARC. Learn more in part 2 of the series originally posted in Cyber Defense …
Integration Management Part 2
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month19 days, 11 hours left to enroll

873 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