-1
我面對我的js/jQuery的一個奇怪的行爲,我在哪裏檢索行的最後一個ID的簡單功能,爲了請檢查是否它是不是從以前的一個不同:
oldMsgId = 0;
// Ajax...
currentId = $(".message_container tr:last").attr('id');
checkNewMsg(currentId);
// ... End Ajax.
function checkNewMsg(MsgId) {
\t if (MsgId != oldMsgId) {
\t \t var snd_msg = new Audio('sound/msg.wav');
\t \t snd_msg.volume = 0.3;
\t \t snd_msg.play();
\t }
\t oldMsgId = MsgId ;
}
該系統的工作,但是,當檢索到沒有新的消息,這意味着最新的ID等於舊的標識,它仍然執行的條件!
當然,它會在頁面加載後執行,因爲oldMsgId設置爲0,但之後通過使用警報測試,它已經顯示條件正在運行,因爲它不應該!
這讓我發瘋,如果有人有一個解決方案,我會很高興聽到這個消息:)
編輯:議決
Well, while using alerts inside the function this time, it appears I have made a huge mistake placing my oldMsgId var inside a loop function (which calls to ajax), so, the variable was reset to 0 everytime and thus made the difference, I'm very sorry xD!
問題解決了,謝謝大家!
向if語句中添加2條顯示MsgId和oldMsgId值的警報,以便直觀地檢查id是否確實相同。 – Barry 2014-10-11 14:22:05
'// Ajax ... // ... End Ajax'好像可能是相關的。您的問題中沒有足夠的信息來給出答案。 – 2014-10-11 14:25:07
顯示更多的代碼,沒有看到這個代碼的上下文,這是任何人的猜測可能是什麼問題 – charlietfl 2014-10-11 14:25:58