$(".pearl").click(function(){
if($(".pearl img").attr("src")=="images/mussel.png"){
$(".pearl img").attr("src","images/mussel2.png");
$(".light").hide()
var leftTime=5*60*60*1000;
function countTime(leftTime){
var d, h, m, s, ms;
if(leftTime >= 0) {
h = Math.floor(leftTime / 1000 / 60 / 60);
m = Math.floor(leftTime / 1000 / 60 % 60);
s = Math.floor(leftTime / 1000 % 60);
ms = Math.floor(leftTime % 1000);
if(ms < 100) {
ms = "0" + ms;
}
if(s < 10) {
s = "0" + s;
}
if(m < 10) {
m = "0" + m;
}
if(h < 10) {
h = "0" + h;
}
} else {
$(".time").text("");
}
$(".time").text(h + "时"+m + "分"+s + "秒")
setTimeout(function(){
leftTime=leftTime-1000;
countTime(leftTime);
}, 1000);
}
countTime(leftTime);
}else{
alert("冷却中")
}
})
这是我做的一个倒计时,现在我想把下面的函数加进去,不再用上面的倒计时了,但是作用不变,下面是代码
var maxtime,minutes,seconds,msg;
if(window.name==''){
testCountDown();
}else{
maxtime=window.name;
testCountDown();
}
function testCountDown() {
$.ajax({
"method":"post",
"url":"${base}/user/countdown.action" ,
"async": "false",//总感觉这里应该用同步,不该用异步。。。你们觉着呢
"data":{ } ,
"success":function (data) {
if (data.seconds){
maxtime = data.seconds;
}else {
alert("空");
}
}
})
}
function CountDown(){
var div = $("#timer");
if(maxtime>=0){
minutes=Math.floor(maxtime/60);
seconds=Math.floor(maxtime%60);
msg="距离倒计时结束还有:"+minutes+"分"+seconds+"秒";
div[0].innerHTML=msg;
if(maxtime==5*60){
alert('距离结束还是有五分钟');
}
--maxtime;
window.name=maxtime;
}else{
clearInterval(timer);
alert("倒计时已经结束了");
}
}
timer=setInterval("CountDown()",1000);
最后是这个倒计时的div
<div class="time"></div>