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

时间:2022-11-05 08:35:34

Bear with me... I'm not really a developer, so I'm sure I haven't gone about this in the best way to begin with, but I'm hoping we can work with what I've got to make it work properly...

忍受我...我不是真正的开发人员,所以我确信我没有以最好的方式开始这个,但我希望我们可以使用我必须做的事情它工作正常......

I've got a combination of js and php that work together to flash pairs of images on the screen, and then capture the key presses for choosing the left or right image (using 'c' and 'm' or the left-arrow and 'right-arrow').

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

It seems to work fine on Safari, Firefox and IE, but is not working consistently in Chrome (sometimes it does, sometimes it doesn't).

它似乎在Safari,Firefox和IE上运行良好,但在Chrome中无法正常工作(有时它确实如此,有时则不然)。

I just looked at the inspector in Chrome while running it, and found that two of the functions weren't being found:

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

Uncaught ReferenceError: imgChoice is not defined (anonymous function)

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

Uncaught ReferenceError: stopChoice is not defined (anonymous function)

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

In my searching for an answer, the closest I got was that it might be that Chrome is processing the flashImages() function too quickly and isn't getting to the other two.

在我寻找答案时,我得到的最接近的可能是Chrome正在快速处理flashImages()函数并且没有进入另外两个。

Is there a way I could fix this? Perhaps integrate the imgChoice and stopChoice functions into the flashImages function?

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

Here are the existing functions:

以下是现有功能:

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

1 个解决方案

#1


2  

Why don't you try delegating the events when you are binding the event handlers.. maybe that might help..

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

$(document).keydown(function(event) { Instead of this try

$(document).keydown(function(event){代替这个尝试

$(document).on('keydown' ,function(event) {

#1


2  

Why don't you try delegating the events when you are binding the event handlers.. maybe that might help..

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

$(document).keydown(function(event) { Instead of this try

$(document).keydown(function(event){代替这个尝试

$(document).on('keydown' ,function(event) {