0

I have a javascript code that generates some elements on the page.But since the data of these newly generated elements comes from an external source,it may take a while for these elements to load. The thing I want to do is that I want to do some things AFTER these elements are loaded.So I decided I should check if the elements have appeared yet every second and then execute my other coeds.

this is what I want to do:

//start a timer like event handler which occurs every second
//in every second{
    if ($(".elementthatmustappear").is(":visible"))
    {
        //stop the timer
        //execute other codes
    }
    else
    {
        //don't stop the timer.continue checking
    }
}

is there a way of doing this?

roostaamir
  • 1,928
  • 5
  • 24
  • 51
  • 1
    Probably a duplicate of: [How to stop a setTimeout loop?](http://stackoverflow.com/questions/8443151/how-to-stop-a-settimeout-loop) – Itay Aug 18 '13 at 13:13

1 Answers1

0

The thing I want to do is that I want to do some things AFTER these elements are loaded.

In this situation it sounds like you should pass a callback to the function loading the content, which should be executed after the load.

If you do decide you want to follow the logic you've mapped out, this should work:

var timer = setInterval(function() {
    if ($(".elementthatmustappear").is(":visible")) {
        clearInterval(timer);
        doSomethingElse()
    }
}, 1000);
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339