简体   繁体   English

未捕获的类型错误:无法读取未定义的属性“indexOf”

[英]Uncaught TypeError: Cannot read property 'indexOf' of undefined

I want the show function outside of document ready.我希望准备好文档之外的显示功能。 If the time is equal to zero it has to show me the right and wrongs, but I was getting an error:如果时间等于零,它必须向我展示对与错,但我收到了一个错误:

Uncaught TypeError: Cannot read property 'indexOf' of undefined未捕获的类型错误:无法读取未定义的属性“indexOf”

$(document).ready(function() {

    droppableId = $(this)[0].id;
    draggableId = ui.draggable[0].id;

    var show = function(ui, droppableId, draggableId) {
        if (wordMap[droppableId].indexOf(draggableId) != -1) {
            ui.draggable.removeClass("ui-state-right").addClass("ui-state-right");
        } else {
            ui.draggable.removeClass("ui-state-right").addClass("ui-state-wrong");
        }
    }
});


var seconds = 60;

function secondPassed() {

    var minutes = Math.round((seconds - 30) / 60),
        remainingSeconds = seconds % 60;

    if (remainingSeconds < 10) {
        remainingSeconds = "0" + remainingSeconds;
    }

    document.getElementById('countdown').innerHTML = minutes + ":" + remainingSeconds;

    if (seconds == 00) {
        clearInterval(countdownTimer);
        document.getElementById('countdown').innerHTML = "0:00";

        if (corrects >= 4) {
            $("#successAlert").show();
            $("#startAgainGameBtn").show();
        } else {
            $("#resultBtn").prop('disabled', true);
            show();
            $("#getSol").show();
        }

    } else {
        seconds--;
    }
}


var countdownTimer = setInterval('secondPassed()', 1000);

Try this code, as in your code you weren't passing the variables into the function hence the undefined error.试试这个代码,因为在你的代码中你没有将变量传递给函数,因此出现了未定义的错误。 This way the variables are ready for direct use in the function.这样,变量就可以直接在函数中使用了。 However this is untested due to not having any html to test with.然而,这是未经测试的,因为没有任何 html 可以测试。

$(document).ready(function() {

    var droppableId = $(this)[0].id;
    var draggableId = ui.draggable[0].id;

    window.show = function() {
        if (wordMap[droppableId].indexOf(draggableId) != -1) {
            ui.draggable.removeClass("ui-state-right").addClass("ui-state-right");
        } else {
            ui.draggable.removeClass("ui-state-right").addClass("ui-state-wrong");
        }
    }
});


var seconds = 60;

function secondPassed() {

    var minutes = Math.round((seconds - 30) / 60),
    remainingSeconds = seconds % 60;

    if (remainingSeconds < 10) {
        remainingSeconds = "0" + remainingSeconds;
    }

    document.getElementById('countdown').innerHTML = minutes + ":" + remainingSeconds;

    if (seconds == 00) {
        clearInterval(countdownTimer);
        document.getElementById('countdown').innerHTML = "0:00";

        if (corrects >= 4) {
            $("#successAlert").show();
            $("#startAgainGameBtn").show();
        } else {
            $("#resultBtn").prop('disabled', true);
            window.show();
            $("#getSol").show();
        }

    } else {
        seconds--;
    }
}


var countdownTimer = setInterval('secondPassed()', 1000);

暂无
暂无

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

相关问题 未捕获的类型错误:无法读取未定义的属性“indexOf” - Uncaught TypeError: Cannot read property 'indexOf' of undefined Vueify + Elixir + Hotloading - Uncaught TypeError:无法读取undefined的属性&#39;indexOf&#39; - Vueify + Elixir + Hotloading - Uncaught TypeError: Cannot read property 'indexOf' of undefined 反应-未捕获的TypeError:无法读取未定义的属性&#39;indexOf&#39; - React - Uncaught TypeError: Cannot read property 'indexOf' of undefined 未捕获的TypeError:无法读取angularjs中未定义的属性&#39;indexOf&#39; - Uncaught TypeError: Cannot read property 'indexOf' of undefined in angularjs 未捕获的类型错误:无法读取未定义对象的属性“indexOf” - Uncaught TypeError: Cannot read property 'indexOf' of undefined with Object 雄辩的Javascript:未被捕获的TypeError:无法读取未定义的属性&#39;indexOf&#39; - Eloquent Javascript: Uncaught TypeError: Cannot read property 'indexOf' of undefined video.load() 未捕获类型错误:无法读取未定义的属性“indexOf” - video .load() Uncaught TypeError: Cannot read property 'indexOf' of undefined TypeError:无法读取Node.js中未定义的属性“ indexOf” - TypeError: Cannot read property 'indexOf' of undefined in Nodejs 类型错误:无法读取未定义的属性“indexOf” - TypeError: Cannot read property 'indexOf' of undefined Javascript TypeError:无法读取undefined的属性'indexOf' - Javascript TypeError: Cannot read property 'indexOf' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM