3

忍受我...我不是真正的开发人员,所以我确定我一开始并没有以最好的方式解决这个问题,但我希望我们可以使用我必须制作的东西它工作正常...

我有一个 js 和 php 的组合,它们一起工作以在屏幕上闪烁图像对,然后捕获用于选择左或右图像的按键(使用'c'和'm'或左箭头和'右箭头')。

它似乎在 Safari、Firefox 和 IE 上运行良好,但在 Chrome 中运行不一致(有时会,有时不会)。

我只是在运行时查看了 Chrome 中的检查器,发现其中两个函数没有找到:

未捕获的 ReferenceError:imgChoice 未定义(匿名函数)

未捕获的 ReferenceError:未定义 stopChoice(匿名函数)

在我寻找答案的过程中,我得到的最接近的结果可能是 Chrome 处理 flashImages() 函数的速度太快了,而没有到达其他两个函数。

有没有办法解决这个问题?也许将 imgChoice 和 stopChoice 函数集成到 flashImages 函数中?

以下是现有功能:

function imgChoice(imgPair)
{
    var imgDataR = '<input type="hidden" name="'+imgPair+'[<?php echo $_POST['topicNumber']; ?>]" value="r" />';
    var imgDataL = '<input type="hidden" name="'+imgPair+'[<?php echo $_POST['topicNumber']; ?>]" value="l" />';
    var noData = '<?php $noData = 1; ?>';
    $(document).keydown(function(event) {
        if (event.keyCode == 67 || event.keyCode == 37) {
            document.getElementById(imgPair+'Data').innerHTML = imgDataL;
            $(document).unbind('keydown');
            }
        if (event.keyCode == 77 || event.keyCode == 39) {
            document.getElementById(imgPair+'Data').innerHTML = imgDataR;
            $(document).unbind('keydown');
        }
    });
    //div = document.getElementById(imgPair);
}
function stopChoice(imgPair)
{
    $(document).unbind('keydown');
}
function flashImages()
{
i=500;
//$('#startTopic').fadeOut(500);
setTimeout("document.getElementById('fullpd').style.cursor='none';",50);
setTimeout("document.getElementById('fullpd').style.background='#464646';",500);

for(x=1;x<=imgPairs.length-1;x++)
    {
    setTimeout("document.getElementById('clickSound').play();",i+2000);
    setTimeout('document.getElementById("'+imgPairs[x]+'").style.display="block";',i+3500)
    setTimeout('imgChoice("'+imgPairs[x]+'");',i+3495)
    setTimeout('document.getElementById("'+imgPairs[x]+'").style.display="none";',i+4000)
    setTimeout('stopChoice("'+imgPairs[x]+'");',i+6000)
    i=i+4000;
    }
setTimeout("document.getElementById('fullpd').style.background='#eaeaea';",i+1000)
setTimeout("document.getElementById('fullpd').style.cursor='default';",i)
setTimeout(function() {$('#endTopic').fadeIn(1000);},i+1000);
}
4

1 回答 1

2

为什么不尝试在绑定事件处理程序时委托事件..也许这可能会有所帮助..

$(document).keydown(function(event) { 而不是这个尝试

$(document).on('keydown' ,function(event) {
于 2012-09-14T21:04:04.220 回答