SitePoint Sponsor

User Tag List

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

    Javascript: calling 2nd function when 1st is don

    I am creating a dynamically generated set of functions that preloads images for a site a category at a time. Basically, there is a doLoad() Function that loads the specified array of images.

    doLoad(imageArray) runs throgh the specified array, loads the images, shows a progress bar, and then enables a link when the funtion is done.

    ( btw, these doLoad statements as well as the image arrays are being dynamically generated)

    My problem is this...

    I cant do this:
    Code:
    function preload(){ 
        doLoad(Category1);
        doLoad(Category2);
        doLoad...;
    }
    because it tries to run them all at once, which turns out to being highly unattractive. I need to run doLoad(Category1), and when it's finished, run doLoad(Category2), and so on...

    i've tried setting a busy = 0 variable or Returning it and then constantly testing for it. This option doesn't work reliably. Is there a way to wait for the end of a function?

    Thanks...
    Happy Turkeys!!!\

    enobrev

  2. #2
    SitePoint Member Ralf-J's Avatar
    Join Date
    Oct 2001
    Location
    Hannover, Germany
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I would do it this way:

    flag=0;

    function preLoad(number,flag)
    {

    if(flag == done)
    {
    switch(number)
    {
    case 1:
    doLoad('Category1',1);
    break;

    case 2:
    doLoad('Category2',2);
    break;

    }
    }
    else
    {
    /* eventually a setTimeout() here?? */
    }
    }

    function doLoad(category,next)
    {
    switch(next)
    {
    case 1:
    do something with category;
    flag=done;
    preLoad(2); /* eventually a setTimeout(preLoad(2),100) ??? */
    break;

    case 2:
    do something with category;
    flag=done;
    preLoad(3);
    break;

    }

    }


    I have not tested it but have used the same princip many times. If it is still loading too fast you could use a setTimeout(preLoad(2)). Eventually you could do it your way by simply using a setTimeout??

    If you want to see something like that with some more features check: www.webwizards.de it's my 'playground'.

    The code is hidden but I had called a setTimeout different times and the damn script always loaded the next page before the setTimeout was finished.

    I have solved it in a similar way like shown above.

    regards

    Ralf-J.

  3. #3
    SitePoint Member
    Join Date
    Nov 2001
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    actually, i ended up doing some thing VERY similar, that seems to have been working right so far (this isn't EXACTLY it, but it's prety damned close to it.:

    Code:
    busy = 0;
    
    function doLoad(loadNumber) {
        if (busy == 1) {
            the_timeout = setTimeout("doLoad(" + loadNumber + ");", 1000);
        } else {
    	busy = 1;
            loadImages(eval("Chapter" + loadNumber));
            loadNumber += 1;
            setTimeout("doLoad(" + loadNumber + ");",1000);
            }
        }
    }
    Your help is VERY much appreciated. If you'd like, I'll post an url when it's up and ready so you can see it in action.

    Thanks again!!

    enobrev

  4. #4
    SitePoint Member Ralf-J's Avatar
    Join Date
    Oct 2001
    Location
    Hannover, Germany
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep,

    looks good. Looking forward to see the site.

    regards

    Ralf-J.

  5. #5
    SitePoint Member
    Join Date
    Nov 2001
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here's the demo site... this isn't the release version (by far)... this is just what i'm showing the cust, to show that it works...

    originally, there were no frames... but due to ns 4 AND 6 issues, i had to split em up...

    http://www.sorighteous.com/subwebs/FrankMarshal


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
  •