Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在chrome应用程序中设置时钟-setTimeout()返回错误_Javascript_Time_Google Chrome Extension_Google Chrome App - Fatal编程技术网

Javascript 在chrome应用程序中设置时钟-setTimeout()返回错误

Javascript 在chrome应用程序中设置时钟-setTimeout()返回错误,javascript,time,google-chrome-extension,google-chrome-app,Javascript,Time,Google Chrome Extension,Google Chrome App,我正在制作chrome应用程序,希望在应用程序的全屏模式下有一个简单的时钟(所以我看不到windows时钟)。我想我会觉得很无聊,因为javascript具有在屏幕上打印当前日期的所有功能。我还用纯javascript实现了这一点(我的意思是在www应用程序中,而不是打包应用程序中)。当我想调用setTimeout()函数时出现了问题-chrome不允许我这样做,并用以下文字警告我: 拒绝将字符串作为JavaScript求值,因为“不安全求值”为 不允许以下内容安全中的脚本源 策略指令:“默认s

我正在制作chrome应用程序,希望在应用程序的全屏模式下有一个简单的时钟(所以我看不到windows时钟)。我想我会觉得很无聊,因为javascript具有在屏幕上打印当前日期的所有功能。我还用纯javascript实现了这一点(我的意思是在www应用程序中,而不是打包应用程序中)。当我想调用setTimeout()函数时出现了问题-chrome不允许我这样做,并用以下文字警告我:

拒绝将字符串作为JavaScript求值,因为“不安全求值”为 不允许以下内容安全中的脚本源 策略指令:“默认src'self'blob:文件系统: chrome扩展资源:“。请注意,“script src”不是显式的 设置,因此“默认src”用作回退

我的代码如下所示:

  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)
功能显示(){
this.currentTimeTxt=“时间:”;
this.getCurrentTime=函数(){
变量时间=新日期()
如果(time.getHours()<10){var hours='0'+time.getHours()}否则{var hours=time.getHours()}
如果(time.getMinutes()<10){var minutes='0'+time.getMinutes()}否则{var minutes=time.getMinutes()}
如果(time.getSeconds()<10){var seconds='0'+time.getSeconds()}否则{var seconds=time.getSeconds()}
返回时数+':'+分钟+':'+秒;
}
}
变量显示=新显示();
setTimeout(console.log(display.getCurrentTime),1000)
在chrome打包的应用程序中,让时钟每秒“滴答滴答”的正确方法是什么

卡尔雷格

----编辑代码:

功能显示(){

this.currentTimeTxt=“时间:”;
this.getCurrentTime=函数(){
变量时间=新日期()
如果(time.getHours()<10){var hours='0'+time.getHours()}否则{var hours=time.getHours()}
如果(time.getMinutes()<10){var minutes='0'+time.getMinutes()}否则{var minutes=time.getMinutes()}
如果(time.getSeconds()<10){var seconds='0'+time.getSeconds()}否则{var seconds=time.getSeconds()}
setTimeout(函数(){display.getCurrentTime}),1000)
返回时数+':'+分钟+':'+秒;
}
}
变量显示=新显示();
display.getCurrentTime()

首先,您需要的是
setInterval
,而不是
setTimeout
setTimeout
仅在指定延迟后执行一次,而
setInterval
每间隔执行一次

setInterval
setTimeout
都使用字符串(这是
eval
'd,通常是一种不好的做法)或函数。
console.log
的返回值不是这两个值中的任何一个。而
console.log
需要
display.getCurrentTime
的返回值,而不是函数本身,因此请记住包含括号

因此,您只需要传入一个获取当前时间的函数,并记录它

通常会向
setInterval
(或
setTimeout
)传递匿名函数,如下所示:

setInterval(function(){
    console.log(display.getCurrentTime());
}, 1000);

首先,您需要的是
setInterval
,而不是
setTimeout
setTimeout
仅在指定延迟后执行一次,而
setInterval
每间隔执行一次

setInterval
setTimeout
都使用字符串(这是
eval
'd,通常是一种不好的做法)或函数。
console.log
的返回值不是这两个值中的任何一个。而
console.log
需要
display.getCurrentTime
的返回值,而不是函数本身,因此请记住包含括号

因此,您只需要传入一个获取当前时间的函数,并记录它

通常会向
setInterval
(或
setTimeout
)传递匿名函数,如下所示:

setInterval(function(){
    console.log(display.getCurrentTime());
}, 1000);
而不是setTimeout(), 建议使用。

而不是setTimeout(),
鼓励使用。

感谢您的解决-间隔和匿名函数是我正在寻找的
getCurrentTime
不应包括对
setInterval
的调用-您希望
setInterval
调用任何应定期执行的代码。感谢您的解决-间隔和匿名函数就是我想要的查找
getCurrentTime
不应包括对
setInterval
的调用——您希望
setInterval
调用任何应该定期执行的代码。