Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


if statement problem in Ajax

Hi, I am making a website where depending on what a column says in the database the javascript will perform a curtain task (using ajax). This is going fine, however in the if statement, when the requirements are met, it keeps repeating it since it's inside the setInterval. It is probably easier for me to just paste the code and give you a chance to take a look at it.



        if ($('#div:contains("hello")').length > 0) {
            alert("Hello, world!");

    }, 200);

As you can see above, the alert is alerted forever at the speed the interval is set to, please help.

Seth Kroger
Seth Kroger
56,412 Points

I think it would be best to first ask yourself when you when you want the if clause to run. Do you want it to run just once? Whenever the result changes? Then you'll have 2 conditions to check: does it contain "hello" and have I run this before/has the value changed from last time?

How would I write those conditions in code?