SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Jul 2009
    Posts
    220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Script to Wait for javascript function to end

    I always tend to have this problem.
    Say I try to do something like that
    Code:
    slide_image();
    get_current_image();
    get_current_image will return the wrong result, because the animation for the slide_image() has not ended.
    Is there a wait function or something?
    setTimeout is not that ideal too since multiple calls stacks up the queue.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,686
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by resting View Post
    get_current_image will return the wrong result, because the animation for the slide_image() has not ended.
    Is there a wait function or something?
    That can depend on the code library that you are using. Which particular image slider are you using?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Addict
    Join Date
    Jul 2009
    Posts
    220
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    That can depend on the code library that you are using. Which particular image slider are you using?
    Actually I wrote my own, based on the tutorial here: http://jonraasch.com/blog/a-simple-jquery-slideshow
    I was using jQuery's animate function and could throw my ending script in the complete method.

    But I keep hitting this problem and wonder how I can over come it by manually injecting a waiting script.
    That is, instead of writing my ending script in the complete method, write it after I call the animate function.
    Its easier to read that way too.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,686
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by resting View Post
    Actually I wrote my own [...] and wonder how I can over come it by manually injecting a waiting script.
    What is normally done is to pass a callback function to the slider, so that when your animation comes to an end, the callback (if any) can then be called.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •