定时器仅30分钟 [英] Timer for only 30 mins
问题描述
我想设置一个计时器,例如我的产品在添加到购物车时应该只显示30分钟。我试过了: -
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)
我知道这是错的。我想用jQuery做到这一点。我是jquery的新手,请帮助我
I know this is wrong. I want to do this using jQuery. I am new to jquery please help me on this
提前致谢。
推荐答案
是的,这有点正确,但我想告诉用户剩余多少时间是
,即倒计时应该开始。怎么做
yeah this somewhat correct but I want to show user how much time is remaining, i.e countdown should start.How to do that
好的,这个怎么样:
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.
或者我建议你使用这些插件:
http://www.tripwiremagazine.com/2012/09/jquery-countdown-scripts.html
Or I recommend you use these plugins: http://www.tripwiremagazine.com/2012/09/jquery-countdown-scripts.html
这篇关于定时器仅30分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!