Link to home
Start Free TrialLog in
Avatar of hidrau
hidrauFlag for Brazil

asked on

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

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
Avatar of hidrau
hidrau
Flag of Brazil image

ASKER

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

SOLUTION
Avatar of Roman Gherman
Roman Gherman
Flag of Moldova, Republic of image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hidrau

ASKER

thanks very much
Avatar of hidrau

ASKER

just one thing,

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

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

Thanks for the points!
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
@roma1123 is right on IE it don't work, just remove the type attribute or replace it with : text/javascript