/suspend.js

A simple wait-to-trigger library that will suspend a function from being activated while an event gets continuously triggered.

Primary LanguageJavaScriptMIT LicenseMIT

Suspend.js

A simple wait-to-trigger library that will suspend a function from being activated while an event gets continuously triggered.

Dependencies

  • There are none

Supports

  • AMD
  • CommonJS
  • Globals

Install

bower install suspend.js

Tutorial

Prevent a function from being actived when keys keep getting pressed

var search_wait = new Suspend();
search_wait.callback(function() {
  console.log("If key was pressed more than once every 200ms then I wouldn't have activated!");
});

document
  .getElementById("search-input")
  .onkeypress = function() {
    search_wait.every(200).start();
  };

Or figure out what to do when the window stops being resized

var responsive_wait = new Suspend();
window.onresize = function(event) {
  responsive_wait
    .callback(function() {
      console.log("If window is resized more than once every 1000ms then I won't activate!");
    })
    .every(1000)
    .start();
};

Delay function until mouse is not longer hovering over either element

var hover_wait = new Suspend({
  element: [
    document.getElementById("box-one"),
    document.getElementById("box-two")
  ],
  event: "onmouseover",
  callback: function(context, e) {
    console.log(context, e);
    console.log("If either box was hovered more than once every " + context.opts.every + "ms then I wouldn't have activated!);
  }
}).start();

API

Suspend(options)

  • options -- object that will override and default settings

.on(element, event)

  • element -- DOM element to apply event to, also accepts an array of elements
  • event -- string specifying the event: "onclick" or "onmouseover", also accepts an array of events

.callback(cb)

  • cb -- function to be activated when the timer expires

.data(dt)

  • dt -- data object that will be passed to the callback function

.every(timer)

  • timer -- integer in miliseconds that determines how long to wait for the event to be triggered before activating the callback function

.start(timer)

  • timer -- integer in miliseconds that determines how long to wait for the event to be triggered before activating the callback function

Initiates Suspend

.trigger(timer)

  • timer -- integer in miliseconds that determines how long to wait for the event to be triggered before activating the callback function

Alias for start()

Credits