Search code examples
javascriptjqueryjquery-uijquery-ui-spinner

how to use two stop event in jquery ui spinner


In the below code I need to execude show_error method with debounce (delay) and show_value method without delay.

Both methods needs in stop event only how to do it ?

$("#test").spinner({
 stop: function(event , ui){
 show_value();
  },
 stop: _.debounce(function(e, ui) {
      show_error();

    }, 300)
        });

Solution

  • Call both functions in a single handler:

    var debouncedStop = _.debounce(function(e, ui) {
          show_error();
    }, 300);
    $("#test").spinner({
     stop: function(event, ui){
         show_value();
         debouncedStop();
     }
    });
    

    Or bind them separately:

    $("#test").spinner()
              .on("spinstop", function(event , ui) {
                  show_value();
              })
              .on("spinstop", _.debounce(function(e, ui) {
                  show_error();
              }, 300));