在 chrome 应用程序中制作时钟 - setTimeout() returns 错误
making clock in chrome app - setTimeout() returns error
我正在制作 chrome 应用程序并希望在应用程序的全屏模式下有简单的时钟(所以我看不到 windows 时钟)。我认为我很简单,因为 javascript 具有在屏幕上打印当前日期的所有功能。我还用纯 javascript 制作了它(我的意思是在 www 应用程序而不是打包的应用程序中)。当我想调用 setTimeout() 函数时出现问题 - chrome 不允许我这样做并警告我:
Refused to evaluate a string as JavaScript because 'unsafe-eval' is
not an allowed source of script in the following Content Security
Policy directive: "default-src 'self' blob: filesystem:
chrome-extension-resource:". Note that 'script-src' was not explicitly
set, so 'default-src' is used as a fallback.
我的代码如下所示:
function display () {
this.currentTimeTxt = "TIME: ";
this.getCurrentTime = function () {
var time = new Date()
if ( time.getHours() < 10 ) { var hours = '0' + time.getHours() } else { var hours = time.getHours() }
if ( time.getMinutes() < 10 ) { var minutes = '0' + time.getMinutes() } else { var minutes = time.getMinutes() }
if ( time.getSeconds() < 10 ) { var seconds = '0' + time.getSeconds() } else { var seconds = time.getSeconds() }
return hours + ':' + minutes + ':' + seconds;
}
}
var display = new display();
setTimeout(console.log(display.getCurrentTime), 1000);
在 chrome 打包应用程序中使时钟每秒“滴答作响”的正确方法是什么?
---- 编辑代码:
function display () {
this.currentTimeTxt = "TIME: ";
this.getCurrentTime = function () {
var time = new Date()
if ( time.getHours() < 10 ) { var hours = '0' + time.getHours() } else { var hours = time.getHours() }
if ( time.getMinutes() < 10 ) { var minutes = '0' + time.getMinutes() } else { var minutes = time.getMinutes() }
if ( time.getSeconds() < 10 ) { var seconds = '0' + time.getSeconds() } else { var seconds = time.getSeconds() }
setTimeout(function () { display.getCurrentTime}, 1000)
return hours + ':' + minutes + ':' + seconds;
}
}
var display = new display();
display.getCurrentTime()
首先,您想要 setInterval
,而不是 setTimeout
。 setTimeout
仅在指定延迟后执行一次,而 setInterval
每隔一段时间执行一次。
setInterval
和 setTimeout
都采用字符串(eval
'd,通常是一种不好的做法)或函数。 console.log
的 return 值不是其中任何一个。而 console.log
需要 display.getCurrentTime
的 return 值,而不是函数本身,因此请记住包含括号。
所以你只需要传入一个获取当前时间的函数,并记录它。
传递setInterval
(或setTimeout
)一个匿名函数是很常见的,像这样:
setInterval(function(){
console.log(display.getCurrentTime());
}, 1000);
而不是 setTimeout(),
chrome.alarm API 鼓励使用。
我正在制作 chrome 应用程序并希望在应用程序的全屏模式下有简单的时钟(所以我看不到 windows 时钟)。我认为我很简单,因为 javascript 具有在屏幕上打印当前日期的所有功能。我还用纯 javascript 制作了它(我的意思是在 www 应用程序而不是打包的应用程序中)。当我想调用 setTimeout() 函数时出现问题 - chrome 不允许我这样做并警告我:
Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "default-src 'self' blob: filesystem: chrome-extension-resource:". Note that 'script-src' was not explicitly set, so 'default-src' is used as a fallback.
我的代码如下所示:
function display () {
this.currentTimeTxt = "TIME: ";
this.getCurrentTime = function () {
var time = new Date()
if ( time.getHours() < 10 ) { var hours = '0' + time.getHours() } else { var hours = time.getHours() }
if ( time.getMinutes() < 10 ) { var minutes = '0' + time.getMinutes() } else { var minutes = time.getMinutes() }
if ( time.getSeconds() < 10 ) { var seconds = '0' + time.getSeconds() } else { var seconds = time.getSeconds() }
return hours + ':' + minutes + ':' + seconds;
}
}
var display = new display();
setTimeout(console.log(display.getCurrentTime), 1000);
在 chrome 打包应用程序中使时钟每秒“滴答作响”的正确方法是什么?
---- 编辑代码:
function display () {
this.currentTimeTxt = "TIME: ";
this.getCurrentTime = function () {
var time = new Date()
if ( time.getHours() < 10 ) { var hours = '0' + time.getHours() } else { var hours = time.getHours() }
if ( time.getMinutes() < 10 ) { var minutes = '0' + time.getMinutes() } else { var minutes = time.getMinutes() }
if ( time.getSeconds() < 10 ) { var seconds = '0' + time.getSeconds() } else { var seconds = time.getSeconds() }
setTimeout(function () { display.getCurrentTime}, 1000)
return hours + ':' + minutes + ':' + seconds;
}
}
var display = new display();
display.getCurrentTime()
首先,您想要 setInterval
,而不是 setTimeout
。 setTimeout
仅在指定延迟后执行一次,而 setInterval
每隔一段时间执行一次。
setInterval
和 setTimeout
都采用字符串(eval
'd,通常是一种不好的做法)或函数。 console.log
的 return 值不是其中任何一个。而 console.log
需要 display.getCurrentTime
的 return 值,而不是函数本身,因此请记住包含括号。
所以你只需要传入一个获取当前时间的函数,并记录它。
传递setInterval
(或setTimeout
)一个匿名函数是很常见的,像这样:
setInterval(function(){
console.log(display.getCurrentTime());
}, 1000);
而不是 setTimeout(), chrome.alarm API 鼓励使用。