-2

I want to set a timer for example my product should be display for only 30 mins when it is added to cart. I tried :-

var d = new Date();

var minute = d.getHours()+30;

Also I want to show user how much time is remaining (countdown)

I know this is wrong. I want to do this using jQuery. I am new to jquery please help me on this

Thanks in advance.

Rakesh Shetty
  • 4,548
  • 7
  • 40
  • 79

3 Answers3

13

yeah this somewhat correct but I want to show user how much time is remaining, i.e countdown should start.How to do that

Ok, how about this:

var countdown = 30 * 60 * 1000;
var timerId = setInterval(function(){
  countdown -= 1000;
  var min = Math.floor(countdown / (60 * 1000));
  //var sec = Math.floor(countdown - (min * 60 * 1000));  // wrong
  var sec = Math.floor((countdown - (min * 60 * 1000)) / 1000);  //correct

  if (countdown <= 0) {
     alert("30 min!");
     clearInterval(timerId);
     //doSomething();
  } else {
     $("#countTime").html(min + " : " + sec);
  }

}, 1000); //1000ms. = 1sec.

Or I recommend you use these plugins: http://www.tripwiremagazine.com/2012/09/jquery-countdown-scripts.html

wf9a5m75
  • 6,100
  • 3
  • 25
  • 59
3

You could take a look at javascript timers here. If you need to time for 30 minutes, try

var timer = window.setInterval(function()
{
    //doSomething
}, 1800000);

EDIT

To keep it from going off again later, use window.clearInterval(timer);

EDIT

If you want, you could keep track of how much time has passed, rather than have one big timer.

var startTime = (new Date()).getTime();
var secondsLeft;
var timer = window.setInterval(function()
{
    var now = (new Date()).getTime();
    secondsLeft = 1800 - ((now - startTime) / 1000);
    //do something here to display secondsLeft
}
Indigenuity
  • 9,332
  • 6
  • 39
  • 68
  • yeah this somewhat correct but I want to show user how much time is remaining, i.e countdown should start.How to do that – Rakesh Shetty Nov 11 '12 at 05:55
  • I hope you don't mind that I changed the link to MDN, which is a better resource than [w3fools](http://w3fools.com/) – Asad Saeeduddin Nov 11 '12 at 05:56
  • You could possibly set an interval for every second to update a display of how much time is left. – Indigenuity Nov 11 '12 at 05:57
  • @Asad, oh no prob. But I probably overwrote it when I edited. My bad – Indigenuity Nov 11 '12 at 05:58
  • Can you show me an example how to achive this ,sorry but I am new to jquery – Rakesh Shetty Nov 11 '12 at 05:58
  • @RakeshShetty Added a brief example. I am using actual time rather than just subtracting one second every time the function runs. If you're doing something else, your function isn't guaranteed to be called on time. – Indigenuity Nov 11 '12 at 06:07
  • yeah it is not subtracting from 30 mins for example if 30 min -- it should show like this 29 mins 59 sec ,29 mins 58 sec etc ... can you please tell me to how to achive this @Indigenuity ? – Rakesh Shetty Nov 11 '12 at 06:09
  • My answer right now reflects how to get how much time is left in seconds. If you want to know how to convert seconds to Hours:Minutes:Seconds, see http://stackoverflow.com/questions/1322732/convert-seconds-to-hh-mm-ss-with-javascript – Indigenuity Nov 11 '12 at 06:11
2

If you want to do it in Javascript, you can do it like this:

var waitTime = 30 * 60 * 1000; // = 30min.

setTimeout(function(){
  alert("30 min!");
}, waitTime);

And if you want to use jQuery, you can use jQuery Timer plugin. http://jquery.offput.ca/every/

But I think if you use PHP and the system is shopping cart(or something), maybe you should do it using session mechanism of PHP.

wf9a5m75
  • 6,100
  • 3
  • 25
  • 59
  • yeah this somewhat correct but I want to show user how much time is remaining, i.e countdown should start.How to do that – Rakesh Shetty Nov 11 '12 at 06:01