Reputation: 109
I have this Javascript/jQuery code:
for (i = 0; i < 100; i++) {
if(!(html == $("#chat-box").html()))
{
$("#chat-box").html(html);
console.log("a");
}
}
I have this iterating on a for loop 100 times. You would think that at least after the 1st time it would stop iterating unless one of the two values changed (which they don't), but it does not. I do not know why. I tried using if(htm1 != ...)
and that did not work.
Upvotes: 0
Views: 49
Reputation: 635
Your variable html
is never being initialized or assigned to. What is it's value? With only the code you provided, it's value is undefined
. So when you try to do this:
$("#chat-box").html(html);
What happens is this:
$("#chat-box").html(undefined);
Which has no effect. But if you were to initialize html
first, it works:
var html = 'stufffff';
for (i = 0; i < 5; i++) {
if(html !== $("#chat-box").html()) {
$("#chat-box").html(html);
console.log("a");
}
}
Also, you should use !==
instead of !=
.
Upvotes: 0
Reputation: 1873
I believe using the text() method for your comparison will area the issue:
if(!(html == $("#chat-box").text()))
{
$("#chat-box").html(html);
}
Reason being is that jQuery processes the text to prevent issues with special characters with html(), where as with text() it returns the raw value. If it still doesn't work, console.log() out both method return values and the comparison variable to better visualize.
Upvotes: 1