Code Monkey home page Code Monkey logo

ecrider / throttle-frequency Goto Github PK

View Code? Open in Web Editor NEW
4.0 0.0 1.0 8 KB

Simple vanilla JavaScript tool to throttle execution frequency of any given function - time in milliseconds, no initial delay, optional scope declaration.

License: MIT License

JavaScript 100.00%
javascript javascript-tools throttle throttle-function throttle-frequency debounce debouncing function-frequency load-balancer loadbalancing throttling delay vanilla-javascript milliseconds

throttle-frequency's Introduction

Throttle Frequency

Simple vanilla JavaScript tool to throttle execution frequency of any given function - time in milliseconds, no initial delay, optional scope declaration.

Installation

Install via npm

$ npm install throttle-frequency

... or yarn

$ yarn add throttle-frequency

then require

var throttleFrequency = require('throttle-frequency');

or import

import throttleFrequency from 'throttle-frequency'

Usage

throttleFrequency(func, delay, scope);

  • param {function} func - function to execute
  • param {number} delay - delay in milisecionds
  • param {object} scope - optional scope in which function will be executed
  • return {function} - wrapped func

Examples

Throttling variable manipulation:

var test;
var testFunc = function(arg) { test = arg; };
var testFuncThrottled = throttleFrequency(testFunc, 500);

testFuncThrottled('TEST 1!');
console.log(test); // 'TEST 1!'

setTimeout(function() {
  testFuncThrottled('TEST 2!');
  console.log(test); // (after 501 ms) 'TEST 2!'
}, 501);

testFuncThrottled('TEST 3!');
console.log(test); // 'TEST 1!'

Emitting debounced window resize event - a little bit more practical example

var triggerEvent = function(name, data) {
  var newEvent = document.createEvent('Event');
  newEvent.initEvent(name, true, true);
  newEvent.data = data;
  window.dispatchEvent(newEvent);
};

var debounceEvent = function(opts) {
  var wrappedEvent = function(event) { triggerEvent(opts.name, event); };
  var debounced = throttleFrequency(wrappedEvent, opts.delay);
  opts.element.addEventListener(opts.type, debounced);
};

debounceEvent({
  element: window,
  type: 'resize',
  name: 'debouncedResize',
  delay: 800
});

window.addEventListener('debouncedResize', function(event) {
  console.log(event); // returns original 'resize' event object
  console.log('Width: ' + window.innerWidth + ' Height: ' + window.innerHeight);
});

Author

Kuba Paczyński

License

Copyright © 2017, Kuba Paczyński. Released under the MIT License.

throttle-frequency's People

Contributors

ecrider avatar

Stargazers

 avatar  avatar  avatar  avatar

Forkers

didyhu

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.