我有这个代码
var clsStopwatch = function() {
var startAt = 0;
var lapTime = 0;
var now = function() {
return (new Date()).getTime();
};
this.start = function() {
startAt = startAt ? startAt : now();
};
this.stop = function() {
lapTime = startAt ? lapTime + now() - startAt : lapTime;
startAt = 0;
};
// Reset
this.reset = function() {
lapTime = startAt = 0;
};
// Duration
this.time = function() {
return lapTime + (startAt ? now() - startAt : 0);
};
};
var x = new clsStopwatch();
var $time;
var clocktimer;
function pad(num, size) {
var s = "0000" + num;
return s.substr(s.length - size);
}
function formatTime(time) {
var h = m = s = ms = 0;
var newTime = '';
h = Math.floor( time / (60 * 60 * 1000) );
time = time % (60 * 60 * 1000);
m = Math.floor( time / (60 * 1000) );
time = time % (60 * 1000);
s = Math.floor( time / 1000 );
ms = time % 1000;
newTime = pad(h, 2) + ':' + pad(m, 2) + ':' + pad(s, 2) + ':' + pad(ms, 3);
return newTime;
}
function show() {
$time = document.getElementById('time');
update();
}
function update() {
$time.innerHTML = formatTime(x.time());
}
function start() {
clocktimer = setInterval("update()", 1);
x.start();
}
function stop() {
x.stop();
clearInterval(clocktimer);
}
function reset() {
stop();
x.reset();
update();
}它有一个秒表。
我的CSP conf有这个unsafe-eval指令。它是不安全的,所以我删除了它。我的代码不能工作,在控制台日志中写道
clocktimer = setInterval("update()", 1);不允许运行。我怎样才能让它在没有unsafe-eval指令的情况下运行?
我不知道该怎么做,因为我对JS真的很陌生。
发布于 2020-12-13 00:28:59
"update()"只是一个字符串,不能调用。您需要传递函数。
// Reference the function (probably better)
clocktimer = setInterval(update, 1)
// Call function with lambda (better in some situations, but not this one)
clocktimer = setInterval(() => update(), 1)https://stackoverflow.com/questions/65267000
复制相似问题