【问题标题】:Strange javascript issue with Google Chrome browserGoogle Chrome 浏览器出现奇怪的 javascript 问题
【发布时间】:2013-11-30 04:21:38
【问题描述】:

基本思路是:页面根据用户的Y或N按键从服务器获取ajax内容。我正在 Chrome V.31.0.1650.57 m 浏览器上测试这个基于 PHP+JS 的 Web 应用程序。我决定测试这个应用程序,并开始按 Y 按钮。它可以工作,但......不是经常,而是在随机时间,chrome 会出现以下错误。

而且php脚本没有问题:它每次都以json编码类型返回答案。我真的找不到问题的原因。有什么建议吗?

代码是这样的

注意:game 是预定义的变量

var answer, question, timer, interval, counter;

$(document).ready(function() {
    startTimer(30);
    getQuestion();
    if (game == 1)
        counter = 5;
    else
        counter = 10;
    $("#show-answer-btn").click(function() {
        $('#res-btn img').fadeOut(1000);
        $('#answer').fadeIn(1000);
    })
})

function playSound(soundfile) {
    document.getElementById("sound").innerHTML =
            "<embed src=\"" + soundfile + "\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";
}

window.onkeydown = function(e) {
    var code = e.keyCode ? e.keyCode : e.which;
    if (code === 89) {
        if (counter == 1) {
            playSound('assets/sounds/answerCorrect.mp3');
            $('#answer').fadeOut(300);
            $('#game-stats').fadeOut(300);
            $('#res-btn img').fadeOut(300);
            $("#green-flash").fadeIn(300);
            $('#question').html("You win");
            $("#green-flash").fadeOut(300);
            setTimeout(function() {
                window.location.href = "/";
            }, 5000);
        }
        else {
            playSound('assets/sounds/answerCorrect.mp3');
            $("#green-flash").fadeIn(300);
            $("#green-flash").fadeOut(300);
            $('#answer').fadeOut(300);
            $('#res-btn img').fadeIn(300);
            getQuestion();
            startTimer(30);
        }

    } else if (code === 78) {
        $("#red-flash").fadeIn(300);
        $("#red-flash").fadeOut(300);
        $('#game-stats').fadeOut(300);
        playSound('assets/sounds/answerWrong.mp3');
        $('#question').html("Wrong");
        setTimeout(function() {
            window.location.href = "/";
        }, 5000);
    }
};


function getQuestion() {
    counter--;
    $.ajax({
        type: "post",
        url: "/question/get",
        data: "diff=" + game,
        dataType: 'json',
        success: function(data) {
            $("#question").html(data.question);
            $("#answer").html(data.answer);
        }
    });

}


function startTimer(timer) {
    clearInterval(interval);
    interval = setInterval(function() {
        timer--;
        $('#timer').html(timer);
        if (timer == 0) {
            clearInterval(interval);
            $('#question').html("Time ended");
            setTimeout(function() {
                window.location.href = "/";
            }, 5000);
            return false;
        }
    }, 1000);
}

【问题讨论】:

    标签: javascript php jquery google-chrome browser


    【解决方案1】:

    我也有这个问题,但在其他方面! 拥有多个计时器会导致许多问题,例如页面渲染速度降低。 您可以将计时器设置为更加相互依赖,而不是独立!

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 1970-01-01
      • 2023-03-13
      • 2013-04-24
      • 2011-12-08
      • 2011-12-19
      • 1970-01-01
      • 2017-12-19
      • 2015-07-08
      相关资源
      最近更新 更多