0

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...') );
});
user1666761
  • 163
  • 2
  • 3
  • 13

4 Answers4

2

Do you want to make it "do something" 4 seconds after you stop dragging it?

If so you want a debounce function. Underscore.js has a good one

Debounce says to execute a function some time after an event stops happening. If you change the value before the time is up, the timer resets. This is very useful when you have flood of events, and only want to do something with the design end value of that flood.

var doSomething = function() { alert('hello!') };
doSomething = _.debounce(doSomething, 4000);

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

  slide: function(event, ui) {
  },
  change: function(event, ui) {
    doSomething();
  });
});

Another simple debounce example: http://jsfiddle.net/bTzTW/

var doSomething = function() {
    alert('fired debounced callback');
};
doSomething = _.debounce(doSomething, 4000);

$('#slider').change(doSomething);
​
Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • It doesn't work for me. Changes are made immediately, not with delay. – user1666761 Nov 30 '12 at 17:39
  • Huh? The change fires immediately, this doesn't change that. This simply delays the `doSomething` function called in response to that change. You need to be more specific about what "not working" means if you want more help. – Alex Wayne Nov 30 '12 at 17:41
  • I want to WAIT 4 seconds to do this console.log()... For example, if I move slider every three seconds the console.log() shouldn't fire at all. Because there will never be 4 seconds with "doing nothing". Your code should work, but there's no delay :( – user1666761 Nov 30 '12 at 17:44
  • I've edited my answer, I wasn't using underscore properly. But you may have to make a JSFiddle showing your problem, but what you say is happening shouldn't be happening. – Alex Wayne Nov 30 '12 at 18:06
  • ok man, thanks for effort. Now it works, check my answer... and thanks especially for that library :) – user1666761 Nov 30 '12 at 18:10
1

Using the javascript built-in setTimeout() and resetting the timeout on change should do the trick:

var timeout; // define somewhere in outer scope

change: function(event, ui) {
    clearTimeout(timeout);
    timeout = setTimeout("console.log('this should show after 4 seconds...')", 4000);
});

or

var timeout;

change: function(event, ui) {
    clearTimeout(timeout);
    timeout = setTimeout(function() {
        // more complex code
    }, 4000);
});
Frank Rupprecht
  • 9,191
  • 31
  • 56
0

If you aren't dead-set on using throttle, setTimeout() works fine.

So you'd have:

change: function(event, ui) {
     setTimeout( function(){console.log('this should show after 4 seconds...')},4000 );
});
Phillip Schmidt
  • 8,805
  • 3
  • 43
  • 67
0

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);
user1666761
  • 163
  • 2
  • 3
  • 13