www.webdeveloper.com
Results 1 to 4 of 4

Thread: Stuck on callback loop issue

  1. #1
    Join Date
    Jun 2014
    Posts
    1

    Stuck on callback loop issue

    So my knowledge on this is really sketchy and I've just started with Javascript so go easy please.

    I've been trying to read up on this but my head can't quite understand whats going on.

    This is the algorithm

    FOR LOOP (i)
    {
    CALL FUNCTION (with param i) WHICH IS ASYNCHRONOUS {
    PUSH TO ARRAY
    }
    }

    The issue here is obvious, the push to array doesn't happen until its completed the full loop cycle, by which the data is lost.

    The function call seems like it must be asynchronous and this I can't change.
    How can i force it to push to the array before getting the next i value.

    If you need more concrete code let me know

  2. #2
    Join Date
    Jul 2013
    Posts
    29
    Hi Monkeyface,

    I am slightly confused with what you're asking so I will try answer as best I can by assuming that you're saying what I am thinking.

    JavaScript "for" loops work by iterating the block of code inside its braces and the counting variable addition is performed at the end of each iteration.

    Taking your algorithm, here is a simple block of code:

    Code:
    function alert(number)
    {
      alert('i = ' + number + ' in this loop');
    }
    
    for(var i=0,i<5,i++)
    {
      alert(i);  
    }
    This would work via:
    • Starts
    • i=0
    • alert function is called (alert(0))
    • loop finishes, i++ (i now =1)
    • starts again at the end of the loop were i=4 it stops



    This is how a standard loop works. Hopefully this is what you need but there is also a possibility that your looking at a run-time error whereby a function is being called on an event call which would need the help of closures but that is a far more complex issue.

    Moreover, you might want to have a look at the "While" and "Do-While" loops, but please only do so if you feel that you fully understand the for loop now. http://www.w3schools.com/js/js_loop_while.asp

    Please shout if this is not what your looking for (and yes, post some code I can look at to fully understand what you need if you do).

  3. #3
    Join Date
    Jul 2013
    Posts
    29
    Also I forgot to mention. the push to the array would work in the same way the call to the function works.
    AnArray.push(i)

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,346
    What you posted is not JavaScript.

    Where did you get it from?
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

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