本文实例为大家分享了js计时器功能的编写代码,供大家参考,具体内容如下
小白一枚,如有代码不规范或者写错的地方,希望得大神指导
代码:
<script src="js/jquery-git.js">
<script type="text/javascript">
//计时
var time = 0;
var a = setInterval(jishi,1000); //1000毫秒
var isCanCancerOrder = false;
function jishi() {
time++;
$('#waitTime').html(calTime(time)); //倒计时
if(time == 60) { //1分钟之后
alert("一分钟到啦!");
}
}
function calTime(time) {
var spit = ":";
var hour = "00";
var second = "00";
var min = "00";
var result = "";
if(time % 60 != 0) { //秒
if(time % 60 > 10) {
second = time % 60;
} else {
second = "0" + time % 60;
}
}
if(parseInt(time / 60) != 0) { //分
if(parseInt(time / 60) > 10) {
min = parseInt(time / 60);
} else {
min = "0" + parseInt(time / 60);
}
}
if(parseInt(time / 3600) != 0) { //时
if(parseInt(time / 3600) > 10) {
hour = parseInt(time / 3600);
} else {
hour = "0" + parseInt(time / 3600);
}
}
result = hour + spit + min + spit + second;
return result;
}