简体   繁体   English

Google Chrome浏览器出现奇怪的JavaScript问题

[英]Strange javascript issue with Google Chrome browser

Basic idea is: The page gets ajax content from server based on Y or N press of user. 基本思想是:页面根据用户的YN键从服务器获取Ajax内容。 I'm testing this PHP+JS based web application on Chrome V.31.0.1650.57 m browser. 我正在Chrome V.31.0.1650.57 m浏览器上测试此基于PHP + JS的Web应用程序。 I decided to test this application, and started to press Y button. 我决定测试此应用程序,并开始按Y按钮。 It works but... not often but in random times, chrome gives following error. 它有效,但...并非经常但在随机时间内,chrome会出现以下错误。

在此处输入图片说明

And there is no problem with php script:it returns answer every time in json encoded type. php脚本没有问题:每次以json编码类型返回答案。 I really can't find the reason of problem. 我真的找不到问题的原因。 Any suggestions? 有什么建议么?

Code looks like that 代码看起来像这样

Note: game is predefined var 注意: game是预定义的var

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);
}

I had this problem too but in other ways! 我也有这个问题,但是在其他方面! Having multiple timers can cause many problems such as page decrease in page rendering speed. 具有多个计时器可能会导致许多问题,例如页面渲染速度下降。 You can set your timers to be more dependently on each other, rather that independent! 您可以将计时器设置为更加相互独立,而不是相互独立!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM