Coder Social home page Coder Social logo

dart-throttle-debounce's Introduction

Dart throttle/debounce

A library for throttling or debouncing frequently occuring events. This package is inspired by jquery throttle/debounce http://benalman.com/projects/jquery-throttle-debounce-plugin

Example

import 'package:throttle_debounce/throttle_debounce.dart';
import 'dart:html' as html;
main() {
    html.Element input = html.querySelector('#inputBox');
    List args = ['argument1', 'argument2'];
    var debouncer = new Debouncer(const Duration(milliseconds:250), callback, args);
    input.onKeyUp.listen((e) {
        debouncer.debounce();
    });
}

void callback(List args) {
    print(args[0]); //will print 'argument1'
    print(args[1]); //will print 'argument2'
}

Throttle

Execute a frequently called function only once in a fixed interval. See http://benalman.com/code/projects/jquery-throttle-debounce/examples/throttle/ for example of throttling.

Usage and Arguments

var throttler = new Throttler(Duration delay, callback, List args, [bool noTrailing]);
element.onKeyUp.listen((e) {
    throttler.throttle();
});

delay - (const Duration) A dart constant of type Duration which.

callback - (Function) A function which is executed after delay milliseconds.

args - (List) argument passed to the callback. (Dart does not support variable number of arguments to a function, so we have to pass the arguments in a list)

noTrailing - (bool) Optional, defaults to false. If noTrailing is true, callback will be called after delay only when the throttle function is being called. If set to false, there will also be one trailing call to the callback after delay after the throttle function has stopped executing.

Debouncing

Debounces the execution of a function. This ensures that the function is executed only once, either at the beginning of a series of calls or at the end. See http://benalman.com/code/projects/jquery-throttle-debounce/examples/debounce/ for example of debouncing.

Usage and Arguments

var debouncer = new Debouncer(Duration delay, callback, List args, [bool atBegin]);
element.onKeyUp.listen((e) {
    debouncer.debounce();
});

delay - (const Duration) A dart constant of type Duration which.

callback - (Function) A function which is executed after delay milliseconds.

args - (List) argument passed to the callback. (Dart does not support variable number of arguments to a function, so we have to pass the arguments in a list)

atBegin - (bool) Optional, defaults to false. If atBegins is true, the callback will be executed at the beginning of the debounced call. If there is a pause of delay duration, callback will again be called when the debounced call starts again. If atBegins is false, callback will be executed whenever there is a pause of delay duration between the debounce calls i.e. at the end.

Licenses

Licensed under MIT

Acknowledgement

Inspired by Jquery throttle/debounce plugin of Ben Alman (http://benalman.com)

dart-throttle-debounce's People

Contributors

disdis avatar shivanshuag avatar

Watchers

 avatar  avatar  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.