views:

74

answers:

2

All,

Is there a jQuery timer which can start a timer for 20 minutes and display time elapsed? Please point me to a small code for it.

var austDay = new getTime();
austDay = new getSeconds(austDay);
var duration = 1200;
duration +=  austDay;

Thanks

+1  A: 

Not sure about a jQuery solution, but it's quite simple to do anyway:

var elapsed = 0;
var interval;
var total = 60 * 20; // 20 mins in seconds

function showElapsedTime()
{
    if(elapsed < total)
    {
        elapsed += 1;

        // If you want you can convert the seconds elapsed to minutes and seconds
        // here before you display them

        $('#elapsed').html(elapsed);
    }
    else
    {
        clearInterval(interval);
        alert('Done');
    }
}

$(function(){
    interval = setInterval(showElapsedTime, 1000);
});

Where #elapsed is a div or span that you want to show the elapse time in.

There are quite a few timer plugins, but they are all just abstractions of setTimeout and setInterval anyway, and I'm not sure they're really much simpler to use.

Mark B
+1  A: 

Here is an example of slighly different behaviour. You can change it to suite your needs.

   1. $("#start").click(function() {
   2.  $("#example_2").everyTime(1000, 'timer2', function(i) {
   3.   $(this).text(i);
   4.  }, 15);
   5. });
   6. $("#stop").click(function() {
   7.  $("#example_2").stopTime('timer2');
   8. });
FractalizeR
You forgot to mention that those functions are part of the Timers plugin (http://plugins.jquery.com/project/timers), and won't work unless you include it.
Mark B
Yep, sorry. They are really required.
FractalizeR