gitbrent / bootstrap4-toggle

Bootstrap 4 Switch Button / Toggle
https://gitbrent.github.io/bootstrap4-toggle/
MIT License
214 stars 67 forks source link

Delay does not work #22

Open xbaha opened 4 years ago

xbaha commented 4 years ago

the code inside the delay function never get excused. if no delay, it's executed, not sure if it's a related bug. but i want to delay the toggle so that my database does not execute a save on each click if someone clicked 100s of times!

    $('#toggle-event').change(function() {

     setTimeout(  function()   {
       $(this).prop('disabled',false);
  }, 1000);

      setTimeout(  function()   {
       $(this).prop('disabled',true);
  }, 10);
})
nikitul commented 4 years ago

I think your code is messing up with this. Here is what I suppose you want:

 $('#toggle-event').change(function() {
    var $elem = $(this);

     setTimeout(  function()   {
           $elem.prop('disabled',false);
      }, 1000);

      setTimeout(  function()   {
            $elem.prop('disabled',true);
       }, 10);
})