0

So I've been googling and no codes really match what I want to acheve what I want to do is have a div lets give it the id of Timer5min then in the div have 5:00 so 5 minutes and 0 seconds then I want to have JQuery take it down second by second I assume that would need a setInterval function and then something like:

 $('#Timer5min').text(--sec);

and then setting a second var like so var sec = $('#Timer5min').text();

I'm not a 100% on how to do the mintues though I assume an if statement but this is where i get stuck.... so any idea's?

It would also be nice if when the timer reaches 0 mintues and 0 seconds to have it redirect somewhere...

Thank you for reading :) No plugin answers please xD

Edit Im looking for something similar to this but with mintues as well...:

<div id="Timer5min">

 300 
</div>

var sec = $('#Timer5min').text()
var timer = setInterval(function() { 
   $('#Timer5min').text(--sec);
   if (sec == 10) {
     //redirect
      clearInterval(timer);
   } 
}, 1000);

http://jsfiddle.net/GNrUM/1181/

Ravvvennna
  • 27
  • 6

3 Answers3

1

I think it would be easier to keep the number of seconds in a variable, rather than encoding/decoding each time.

var i=300;
document.getElementById("xxx").innerHTML=""+Math.floor((i/60))+":"+(i%60);
AMADANON Inc.
  • 5,391
  • 18
  • 30
  • Ended up with one via the answer up top but can't get it to work in this http://codepen.io/anon/pen/GgoeQZ any ideas? – Ravvvennna Dec 10 '14 at 02:41
  • Try http://codepen.io/anon/pen/vEGYMG - a lot simpler! Also, I don't think codepen.io supports jquery; you might try jsfiddle instead. Not that anything here needs jquery. – AMADANON Inc. Dec 10 '14 at 21:55
1

This should get you what you are looking for. One thing to remember with this solution though, setTimeout isn't guaranteed to fire exactly at 1000 milliseconds so your timer may occasionally be off by a little bit.

(function() {
    var timer = 301; // 5 minutes worth of seconds + 1 for the first call

    function countDown() {
        if (--timer) {
            var minutes = timer % 60;
            if (!minutes) {
                minutes = '00';
            }
            $('#Timer5min').text(Math.floor(timer/60) + ':' + minutes);
            setTimeout(countDown, 1000);
        } else {
            window.location = 'http://google.com';
        }
    }

    countDown();
})();
rdubya
  • 2,886
  • 1
  • 13
  • 20
  • @Ravvvennna Your jQuery selector needs to be '.loader .message.phrase4.helvetica'. Currently it isn't finding anything so your check never evaluates to true. – rdubya Dec 11 '14 at 09:51
0

I would say get the system time when you want the timer to start and then check to see if the minutes are 5 more than when you started

How to get current time with jQuery

Community
  • 1
  • 1
FFF
  • 591
  • 1
  • 6
  • 15
  • I don't really want to be messing around with the real date and time just something simple that counts down from 5 minutes. thank you for your answer though :) – Ravvvennna Dec 10 '14 at 01:56
  • Im not talking about changing the date/time im just saying setting a value to the current time and checking when it's been 5 minutes. – FFF Dec 10 '14 at 01:58
  • there is also this: setTimeout(function() { // Do something after 3 seconds }, 3000); – FFF Dec 10 '14 at 01:58
  • I know but im trying to make a div that has 5:00 and then count it down with setInterval the problem is the miinutes if i want just the seconds to count I could do var sec = $('#Timer5min').text() var timer = setInterval(function() { $('#Timer5min').text(--sec); if (sec == 10) { //redirect clearInterval(timer); } }, 1000); and set the div to 300 – Ravvvennna Dec 10 '14 at 02:00