Javascript:“!=”运算符无法正常运行

时间:2014-10-11 14:17:40

标签: javascript conditional operator-keyword behavior

晚上好!

我正在使用我的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!

问题解决了,谢谢大家!

1 个答案:

答案 0 :(得分:0)

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

问题解决了,谢谢大家!