• 0

jQuery - wait until function is finished


Question

I have a slider which on click of a link performs a function. The problem I have is if you click it multiple times it goes haywire. I need to tell the function to not perform any calculations until the previous is done. Or just not perform anything at all if a function is currently animating. I know there is a callback function, but I'm not sure if this is the right thing to use and how to use it. Can anyone enlighten me?

8 answers to this question

Recommended Posts

  • 0

Hmmm, not sure if I'm fulling wrapping my head around this. I get what it can do, but is this only for animations? What I have is a function that calculates the current position and how far it has to move the object. The problem is if you click the arrow more than once it calculates based off of the position the animation is currently at. I need it to wait until the function is done before performing the next.

Here's an example. While it's not the exact same thing I'm currently working on, it has the same issue. Try click next multiple times... http://mirrorcreative.net/

  • 0
  Mike said:
Something simple like this:

var running = false;
function foo()
{
  if (running == true) return;

  running = true;

  // the code

  running = false;
}

In addition to Mike's post since this is something that will need to be active for the next click you can use a setTimeout to return running to false and set the timer to time required for the animation to complete.

At the moment it appears you are keeping track of position using the scroll left value. This can be a problem if the next click happens in the middle of an animation as it is right now. Sometimes it can be a problem to make the user wait till the animation completes since they may want to take a quick look. So I would recommend recording the stop positions when the dom is ready and then keeping track of clicks to decide where to stop.

  • 0
  sweetsam said:
At the moment it appears you are keeping track of position using the scroll left value. This can be a problem if the next click happens in the middle of an animation as it is right now. Sometimes it can be a problem to make the user wait till the animation completes since they may want to take a quick look. So I would recommend recording the stop positions when the dom is ready and then keeping track of clicks to decide where to stop.

Exactly right. Can you explain your recommendation a bit further?

  • 0

Say you have three div's with widths 100, 150, 200 pixels. Setup a function that monitors number of clicks which resets to zero in this case after 2 clicks. Setup an array with stops when the dom is ready as below...

1 -> 100

2 -> 250

where first column is clicks and second column is scroll left values. So now click counter will control the scrolling and the counting is instantaneous avoiding errors in detecting current state. I hope I make sense.

  fall3n said:
Exactly right. Can you explain your recommendation a bit further?
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • I'd say "no thank you" to Recall. Let's turn it off as quicly as possible when installed. And Microsoft -please- don't let a "bug" re-install/re-configure it again.
    • Let's just wait until Patch Tuesday and have most of the bugs on this KB/download be taken care of & ironed out.
    • Just read that before coming here. So sad 😪 Always been one of my all time favorites whether with Sabbath or solo Glad he got the chance to do his 1 last show. Truly going home now.  
    • It comes just weeks after the Black Sabbath star took to the stage one final time with his band mates at Villa Park in Birmingham. He died "surrounded by love", a statement from his family said on Tuesday night. The family said: "It is with more sadness than mere words can convey that we have to report that our beloved Ozzy Osbourne has passed away this morning. "He was with his family and surrounded by love. We ask everyone to respect our family privacy at this time. "Sharon, Jack, Kelly, Aimee and Louis."     https://www.thesun.co.uk/tvandshowbiz/23619758/ozzy-osbourne-dead-black-sabbath-parkinsons/
    • I hard disagree on Edge being great. I actually spent a lot of time on Internet Explorer, even in the days when IE was basically a dead system and most had already switched to Chrome. I kept using IE because I was basically a Microsoft-fan-boy back then. When I first started using Windows 10, I of course started using Edge. Some things were a lot more responsive, but it had so many issues it basically was unusable. I think it's a pretty big condemnation when someone who was fine using IE11 says that Edge is what finally drove me to start using Chrome. Yes, Edge did get better over its few years before it was replaced, but it had already failed in the minds of most users.
  • Recent Achievements

    • Week One Done
      SmileWorks Dental earned a badge
      Week One Done
    • Community Regular
      vZeroG went up a rank
      Community Regular
    • Collaborator
      Snake Doc earned a badge
      Collaborator
    • Week One Done
      Snake Doc earned a badge
      Week One Done
    • One Month Later
      Johnny Mrkvička earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      586
    2. 2
      Michael Scrip
      197
    3. 3
      ATLien_0
      196
    4. 4
      +FloatingFatMan
      131
    5. 5
      Xenon
      122
  • Tell a friend

    Love Neowin? Tell a friend!