GithubHelp home page GithubHelp logo

isabella232 / deferred-event-callback Goto Github PK

View Code? Open in Web Editor NEW

This project forked from aoepeople/deferred-event-callback

0.0 0.0 0.0 21 KB

Calling event callbacks with a delay. Supports jQuery.

JavaScript 100.00%

deferred-event-callback's Introduction

Build Status

deferred-event-callback

Calling event callbacks with a delay. Supports jQuery.

How

deferredEventCallback(options);

options (object)

option mandatory type defaultValue description
eventNames Array - The event names to which the node(s) should listen
nodes Array - The nodes to which the listeners should be bound
timeoutValue Number 300 How long to wait before calling the callback
jquery function - Pass in jQuery instance to use jQuery event binding
callback function - The callback is called after the specified timeoutValue. The timeout gets the node passed in on that the event has been triggered. The passed in node is not selected with jquery, even if options.jquery is a jquery instance.

Example

deferredEventCallback({timeoutValue: 200, eventNames: ['resize'], nodes: [window], jquery: jQuery, callback: function() {
    // executed with 200ms delay when resize event triggered on window and only once (if no other 
    // resize event triggered on window for 200 ms)
}});

Stopping callback triggering

Sometimes it might be useful to stop the triggering of the deferred callback. This can be done by calling abort on the instance:

    var instance = lib({eventNames: ['keypress', 'keyup'], nodes: [node, node, node], jquery: $, timeoutValue: 50, callback: function() {}});

    // later do
    instance.abort();

Why

When using events like 'resize' you don't want to call your callback function everytime the browser triggers the resize event (it's quite often). Instead you want to wait some milliseconds and see if the resize is still in progress.

When using events like 'input' and 'propertychange' for stuff like livesuggestions you don't want to trigger a xhr everytime input / propertychange triggers. Instead you want to wait some milliseconds and see if the user is typing another letter.

deferred-event-callback's People

Contributors

alex-voigt avatar mdix 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.