I'm losing the elements of an array. What to do now?


I would like to know why, in the following code, the elements of coupleFound array are deleted when the function set Interval dies. The idea is, get into the a element and check her tag name ai.

When he accept this tag he makes a copy of the entire element starting from his parent, and put into the coupleFound array. It works, but only inside of set Interval function! I don’t understand because i declared the array outside of the function! I believe it is because setInterval is not synchronized, but i don’t know how fix this.

    var clicked = 0,
        totalClicks = 3,
        index = 0,
        listIds = new Array("289657", "2680235", "1597254", "269621"),
        coupleFound = new Array( ),
        videos = document.getElementById( "videos_list" );
    var interval = setInterval(function( ) {
        coupleList = videos.getElementsByTagName( "a" );
        for(var i = coupleList.length; i--;) {
            for(j=0; j < listIds.length; j++) {
                if(coupleList[i].getAttribute( "ai" ) == listIds[j]) {
                    coupleFound[index] = coupleList[i].parentNode;
        videos.removeChild( videos.lastChild );

    if(clicked >= totalClicks) {
        clearInterval( interval );
        alert("I'm inside of the function. The length is:" + coupleFound.length)
    }, 1000);

    alert("The length of the array is:" + coupleFound.length);

What can i do to solve this problem?

Hi robgeek,

It looks like someone already answered your question over at StackOverflow - the problem is that the last alert call is executed before your setInterval callback. As for how you should fix it, that all depends on what you’re trying to do.