Giter Club home page Giter Club logo

kashkin / debounce-event Goto Github PK

View Code? Open in Web Editor NEW

This project forked from goodtimeaj/debounce-event

0.0 1.0 0.0 16 KB

Debounces batched browser event callback execution until the last event within a given time period. The utility provides support for attaching multiple callbacks to the same debounced event batch, manual detaching of batches, and the option to execute callbacks only once.

License: MIT License

JavaScript 94.57% HTML 5.43%

debounce-event's Introduction

Debounce Event

Debounces batched browser event callback execution until the last event within a given time period. The utility returns a key upon initial event attachment which can be later used to add multiple callbacks to the same debounced event batch by given element, event name, and timeout. Debounced event batches can be manually detached, or will detach automatically if the callback is specified to be executed only once and there are no other callbacks left in the event batch.

Usage

Include

Script include creates global DebounceEvent:

<script src="debounce-event.min.js"></script>

AMD:

<script src="debounce-event.min.js"></script>
<script>
  define(['debounce-event'], function(DebounceEvent) {
    // do something
  })
</script>

CommonJS:

$ npm install debounce-event
var DebounceEvent = require('debounce-event')

API

Debounce Initializer Parameters

DebounceEvent.attach() takes an object with the following named parameters:

Name Description Required
elem DOM element to attach the event handler. if eventKey not given
eventName Name of the browser event to handle. if eventKey not given
done Callback to execute on the debounced event. always
timeout Timeout (ms) at which to debounce the event callback. always
once True if the callback should execute only once. If the callback is the only one in its batch, its handler will be detached after the callback executes. Default is false. no
eventKey Previously returned key of an event batch to add this callback. If this parameter is specified, eventName and elem are not used since an event batch uses a common event handler. no

Example

// Register the first event in a new event batch, set to execute only once
var eventKey = DebounceEvent.attach({
  eventName: 'resize',
  elem: window,
  done: function() {
    console.log('done 1')
  },
  timeout: 200,
  once: true
})

// Register the second event in an existing event batch given the previous
// event key
DebounceEvent.attach({
  eventKey: eventKey1,
  done: function() {
    console.log('done 2')
  },
  once: true
})

// Register the third event in a new event batch
var eventKey2 = DebounceEvent.attach({
  eventName: 'resize',
  elem: window,
  done: function() {
    console.log('done 3')
  },
  timeout: 300
})

// Manually detach the given event batch
DebounceEvent.detach(eventKey2)

Development

$ cd debounce-event
$ npm install
$ grunt install
$ grunt build
$ grunt test

# Build for release
$ grunt dist

License

Copyright (c) 2016 Greg Stallings. See LICENSE for details.

debounce-event's People

Contributors

goodtimeaj avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.