Chrome Web App:无法在游戏循环中执行功能

时间:2012-11-02 03:05:53

标签: 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()

1 个答案:

答案 0 :(得分:4)

不知道这是否有帮助,但请尝试以下操作:替换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,并被视为不良做法。