javascript - Chrome网络应用程序: Cannot execute function in game loop

标签 javascript google-chrome content-security-policy

我是 Chrome Web Apps 的初学者,正在尝试将 Bump'n'Jump 的 JavaScript/HTML5 克隆打包为打包应用程序。我在 Chrome 的安全政策方面遇到了困难:

function pump() {
    while (1) {

        game_loop();
        var now = timeGetTime();
        var time_diff = next_time - now;
        next_time += (1000 / 60);

        if (time_diff>0) {
            // we have time left
            setTimeout("pump()", time_diff);
            break;
        }
        // debug("frametime exceeded: " + (-time_diff));
    }
}

它拒绝运行setTimeout("pump()", time_diff);,说

Refused to evaluate script because it violates the following Content Security Policy      directive: "default-src 'self' chrome-extension-resource:". Note that 'script-src' was not explicitly set, so 'default-src' is used as a fallback.

错误不在于代码,因为它在 Chrome 之外的浏览器中运行,Chrome 的安全性似乎不喜欢它。谁能告诉我为什么它不想运行 pump()

最佳答案

不知道这是否有帮助,但请尝试以下操作:替换 setTimeout 字符串参数:

function pump() {
    while (1) {

        game_loop();
        var now = timeGetTime();
        var time_diff = next_time - now;
        next_time += (1000 / 60);

        if (time_diff>0) {
            // we have time left
            setTimeout(pump, time_diff);
            break;
        }
        // debug("frametime exceeded: " + (-time_diff));
    }
}

setTimeoutsetInterval 中使用字符串作为执行参数相当于 eval,被认为是不好的做法。

关于javascript - Chrome网络应用程序: Cannot execute function in game loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13188726/

相关文章:

javascript - 单击时使用 Javascript 加载图像

javascript - 查找跨时区的兼容时间

javascript - 如何运行占用大量 CPU 和内存的 HTML/javascript 程序? (Firefox/Chrome 崩溃)

javascript - Puppeteer 在当前窗口而不是新窗口中启动一个新选项卡

javascript - 使用 ng-template 清空 native 元素

javascript - 将 google map API 标记限制为每个标记只能点击一次

google-chrome - 如何在“网络”选项卡中查看返回 303 重定向的 POST 请求的 POST 参数

html - 将谷歌字体 (fonts.googleapis.com) 添加到 CSP header

java - 静态引用 WebSecurityConfigurerAdapter 来获取随机 CSP 随机数哈希?

iframe - 内容安全策略、X-Frame-Options 和 localhost