本帖最后由 bb8820 于 2019-11-1 10:08 编辑
先停掉页面内的定时器,这里用了比较暴力的方法,而且不一定能成功,如果时间乱跳,就说明没成功,需要把开始的循环里面的1000改大一点。
for(var i = 1; i < 1000; i++) {//如果时间乱跳,把这里的1000再改大一点
clearInterval(i);
}
var a=document.getElementsByClassName("op-beijingtime-time")[0]
var text=a.innerText;
var reg1 = /^[0-9]+/;
var reg2 = /[0-9]{4}/;
var hours = text.match(reg1).toString();
var miniutes =text.match(reg2).toString().slice(0,2);
var senconds =text.match(reg2).toString().slice(2,4);
var addS = 10;//在真实时间基础上增加(正数)或减少(负数)的秒数
var addM = 10;//在真实时间基础上增加(正数)或减少(负数)的分钟数
var addH = 1;//在真实时间基础上增加(正数)或减少(负数)的小时数
var h = parseInt(hours) + addH;
var m = parseInt(miniutes) + addM;
var s = parseInt(senconds) + addS;
if(s > 59){
m = m + 1;
s = 0;
}
if(m > 59){
h = h + 1;
m = 0;
}
if(h > 23){
h = 0;
}
if(s<10){
senconds = "0" + s;
}else{
senconds = "" + s;
}
if(m<10){
miniutes = "0" + m;
}else{
miniutes = "" + m;
}
if(h<10){
hours = "0" + h;
}else{
hours = "" + h;
}
var html = hours + "<span>:</span>"+miniutes + "<span class='op-beijingtime-small c-gap-left'>"+senconds + "</span>";
a.innerHTML = html;
setInterval(setTime = function(){
s = parseInt(senconds) + 1;
if(s > 59){
m = m + 1;
s = 0;
}
if(m > 59){
h = h + 1;
m = 0;
}
if(h > 23){
h = 0;
}
if(s<10){
senconds = "0" + s;
}else{
senconds = "" + s;
}
if(m<10){
miniutes = "0" + m;
}else{
miniutes = "" + m;
}
if(h<10){
hours = "0" + h;
}else{
hours = "" + h;
}
html = hours + "<span>:</span>"+miniutes + "<span class='op-beijingtime-small c-gap-left'>"+senconds + "</span>";
a.innerHTML = html;
},1000);
|