SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Nov 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    sandysafada
    http://stackoverflow.com/questions/2...ray-what-to-do

    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.

    Code:
        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;
                        index++;
                        break;
                    }
                }
            videos.removeChild( videos.lastChild );
        }
    
        document.getElementById('btnMoreVideos').click();
        clicked++;
        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?

  2. #2
    SitePoint Mentor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,257
    Mentioned
    32 Post(s)
    Tagged
    5 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •