gpt4 book ai didi

javascript - 如何在倒数计时器javascript中显示毫秒数

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:37:57 24 4
gpt4 key购买 nike

我有一个 javascript 代码,它显示一个从 5 分钟开始的倒数计时器。

这是代码

var mins
var secs;

function cd() {
mins = 1 * m("05"); // change minutes here
secs = 0 + s(":00"); // change seconds here (always add an additional second to your total)
redo();
}

function m(obj) {
for(var i = 0; i < obj.length; i++) {
if(obj.substring(i, i + 1) == ":")
break;
}
return(obj.substring(0, i));
}

function s(obj) {
for(var i = 0; i < obj.length; i++) {
if(obj.substring(i, i + 1) == ":")
break;
}
return(obj.substring(i + 1, obj.length));
}

function dis(mins,secs) {
var disp;
if(mins <= 9) {
disp = " 0";
} else {
disp = " ";
}
disp += mins + ":";
if(secs <= 9) {
disp += "0" + secs;
} else {
disp += secs;
}
return(disp);
}

function redo() {
secs--;
if(secs == -1) {
secs = 59;
mins--;
}

var timerStr = dis(mins,secs);
$("#countDownTimer").text(timerStr); // setup additional displays here.
if((mins == 0) && (secs == 0)) {

} else {
cd = setTimeout("redo()",1);
}


}

function init() {
cd();
}

window.onload = init;

我怎样才能改变脚本来显示毫秒

或者是否有任何简单的脚本来显示倒数计时器,包括分钟:秒:毫秒

最佳答案

我修改了代码以包含毫秒。还要确保 setTimer 设置正确。

var mins
var secs;
var ms;

function cd() {
mins = 1 * m("05"); // change minutes here
secs = 0 + s(":00"); // change seconds here (always add an additional second to your total)
ms = 0 + ms(":00");//change millisecons here
redo();
}

function m(obj) {
for(var i = 0; i < obj.length; i++) {
if(obj.substring(i, i + 1) == ":")
break;
}
return(obj.substring(0, i));
}

function s(obj) {
for(var i = 0; i < obj.length; i++) {
if(obj.substring(i, i + 1) == ":")
break;
}
return(obj.substring(i + 1, obj.length));
}

function ms(obj) {
for(var i = 0; i < obj.length; i++) {
if(obj.substring(i, i + 1) == ":")
break;
}
return(obj.substring(i + 1, obj.length));
}

function dis(mins,secs,ms) {
var disp;
if(mins <= 9) {
disp = " 0";
} else {
disp = " ";
}
disp += mins + ":";
if(secs <= 9) {
disp += "0" + secs;
} else {
disp += secs + ":";
}
if(ms <= 9) {
disp += "0" + ms;
} else {
disp += ms;
}
return(disp);
}

function redo() {
ms--;
if(ms == -1) {
ms = 99;
secs--;
}
if(secs == -1) {
secs = 59;
mins--;
}

var timerStr = dis(mins,secs,ms);
document.getElementById('countdown_fld').innerText=timerStr; // setup additional displays here.
if((mins == 0) && (secs == 0) && (ms == 0)) {

} else {
cd = setTimeout("redo()",10); //make sure to set the timer right
}


}

function init() {
cd();
}

关于javascript - 如何在倒数计时器javascript中显示毫秒数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8067488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com