0

我想讓文檔之外的顯示功能準備就緒。如果時間等於零,它必須向我證明正確與錯誤。但我得到一個錯誤**遺漏的類型錯誤:無法讀取的未定義的屬性「的indexOf」 **Uncaught 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

你可以顯示你的HTML嗎? –

+0

嘗試使'show()'窗口變量,所以它會'window.show = function(){}'然後你會使用window.show引用它 – Deckerz

+0

錯誤的原因是'wordMap [droppableId]'做不提及任何東西。你期待它成爲什麼? –

回答

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); 
+0

其實這是一個巨大的編碼它,我創建可拖動的元素動態,如果你想看到我會發布在這裏 – babuharry

相關問題