1

var slideContainer = ('#slider');  
var width = 720;

function slide () {  
  if ( parseInt( $slideContainer.css('marginLeft') ) >= -2160 ) {
    $slideContainer.animate({'margin-left': '-='+width},200);
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class='button' onclick='slide()'></a>

I have this simple function, the problem is when I click multiple clicks on the button with animation speed more than 150 it take the slider container more to the left more than -2160 the limit in my if condition .

4
  • you can use .one() like $('.button').one(function(){}) Commented May 28, 2018 at 2:51
  • you can also disable the button on click and enable it again anytime you want Commented May 28, 2018 at 3:06
  • Possible duplicate of Disable multiple clicks JavaScript Commented May 28, 2018 at 3:21
  • how can i disable the button duing the animation ? Commented May 28, 2018 at 17:26

2 Answers 2

1

var slideContainer = ('#slider');  
var width = 720;
var clicked = false;

function slide () {  
      if ( clicked ) return;
      clicked = true;
      if (parseInt( $slideContainer.css('marginLeft') ) >= -2160 ) {
        $slideContainer.animate({'margin-left': '-='+width},200,"linear",()=>{
clicked=false;});
       }
}
<a class='button' onclick='slide()'></a>

Sign up to request clarification or add additional context in comments.

5 Comments

explain bit of your approach then answer has more value
Use the animate's callback function, when the animation is end, the click not return,else slide break ;
the explanation add to the answer then it more clear. I don't want any explanation. I want to improve your answer :)
OK, this is a simple case, there are many solutions to this problem;
actualy i want the button works only after the animation is done ... i don't want clicks while the animation speed ( 200 ) any way to do this ?
0

You can achieve this in either ways-

  1. You can use flag variable to indicate that animation is still in process- initially set false then set true when animation starts and false on animation completion , use this flag with & expression in if.
  2. Little complex but still solves the purpose , you can try this- Inside function slide write the following snippet-

var events = $._data(sliderContainer[0], 'events');
                        handler = events.click;
                        events.click = null;

slideContainer.animate({'margin-left': '-='+width},{duration: 200, complete:function(){evends.click = handler }});

Hope this helps..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.