Search code examples
jquerydelayjquery-ui-slider

Do something after n seconds after moving slider


I want to process value of slider with delay. So If I move with slider to X position, I want to make jquery function to do something BUT, after n seconds. If I do something between these 4 seconds again - start timer from begining.

$('#slider1').slider({
      min: 0,
      max: diff,
      range: true,
      values: [diff-2 , diff],

      slide: function(event, ui) {
      },
      change: function(event, ui) {
         // n SECONDS after changing slider's value do something...
      });

I found:

$.throttle();

but It doesn't work, or I don't know how to use it.

Tried to add to the function succes, like this, but with no luck:

change: function(event, ui) {
     $.throttle( 4000, console.log('this should show after 4 seconds...') );
});

Solution

  • Debounce should be applied to whole function...

    $('#slider1').slider({
          min: 0,
          max: diff,
          range: true,
          values: [diff-2 , diff],
    
          slide: function(event, ui) {
          },
          change: _.debounce(function(event, ui) {
             console.log('just after four seconds');
             // now it works
          },4000);