我想在文档之外准备好show函数。如果时间等于零,它必须向我显示正确和错误。但我得到一个错误**未捕获TypeError:无法读取未定义的属性'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);
答案 0 :(得分:0)
尝试使用此代码,因为在代码中您没有将变量传递给函数,因此未定义错误。这样变量就可以直接在函数中使用了。然而,这是未经测试的,因为没有任何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);