dcsimg
www.webdeveloper.com
Results 1 to 3 of 3

Thread: Call a function inside a loop

  1. #1
    Join Date
    Dec 2010
    Posts
    2

    Call a function inside a loop

    I want to call a function inside a loop of an array but when I run it, it is only reaching the function the first time.

    for (i in qMedia) {
    writeMedia(qMedia[i].name, qMedia[i].format);
    }

    Even when I tried to call it outside the loop multiple times, it only executed once.

    writeMedia(qMedia[0].name, qMedia[0].format);
    writeMedia(qMedia[1].name, qMedia[1].format);
    writeMedia(qMedia[2].name, qMedia[2].format);

    I know that each of the objects in the array have data because I tried to run each one of the lines above separately without calling the other two and it worked fine.

  2. #2
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Quote Originally Posted by margalits View Post
    I want to call a function inside a loop of an array but when I run it, it is only reaching the function the first time.

    for (i in qMedia) {
    writeMedia(qMedia[i].name, qMedia[i].format);
    }

    Even when I tried to call it outside the loop multiple times, it only executed once.

    writeMedia(qMedia[0].name, qMedia[0].format);
    writeMedia(qMedia[1].name, qMedia[1].format);
    writeMedia(qMedia[2].name, qMedia[2].format);

    I know that each of the objects in the array have data because I tried to run each one of the lines above separately without calling the other two and it worked fine.
    Look for JavaScript errors. If there is one, is it in the loop? Or is it in the function that you are calling?

    ^_^

  3. #3
    Join Date
    Dec 2010
    Posts
    2
    Thank you! There was a Javascript error. I am new at this, I found the Javascript console and saw the error. The code I posted was a very very simplified version of what I'm doing. It was actually supposed to populate smarty code and because of another problem, it wasn't populating so the html generated was missing things and creating syntax errors and not executing anything after the first call of this writeMedia function.

    Not sure if that made total sense but it works now. Thanks again for the simple but helpful pointer.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles