Javascript:"!="操作员不'工作不正常

Javascript: " != " Operator doesn't work properly

本文关键字:quot 工作 不正常 Javascript 操作员      更新时间:2023-09-26

晚上好!

我的js/jquery面临着一个奇怪的行为,对于一个简单的函数,我检索一行的最后一个ID,以检查它是否与前一个不同:

oldMsgId = 0;
// Ajax...
currentId = $( ".message_container tr:last" ).attr('id');
checkNewMsg(currentId);
// ... End Ajax.
function checkNewMsg(MsgId) {
	if (MsgId != oldMsgId) {
		var snd_msg = new Audio('sound/msg.wav');
		snd_msg.volume = 0.3;
		snd_msg.play();
	}
	oldMsgId = MsgId ;
}

然而,系统可以工作,当没有检索到新消息时,这意味着最新的ID等于旧的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!

问题解决了,谢谢大家!

好吧,这次在查看函数内部的警报时,我似乎犯了一个巨大的错误,将我的oldMsgId var放在了一个循环函数(它调用ajax)中,对不起xD!

问题解决了,谢谢大家!