-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