on3iro/functionstein

View on GitHub
docs/-_tests__throttle.test.js.html

Summary

Maintainability
Test Coverage
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JSDoc: Source: _tests_/throttle.test.js</title>

    <script src="scripts/prettify/prettify.js"> </script>
    <script src="scripts/prettify/lang-css.js"> </script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>

<body>

<div id="main">

    <h1 class="page-title">Source: _tests_/throttle.test.js</h1>

    



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>// @flow

/**
 * Makes sure that a given function is only called once in a specified
 * timeframe.
 * @function throttle
 * @param {function} fn - Function to call after timeout
 * @param {number} time - Timeout in ms
 * @param {object} context - Useful for throttling object methods
 * @return {function} Throttled function
  */
export const throttle = (fn: Function, time: number, context: Object = this): Function => {
  let timeout
  let callbackArgs

  const callback = () => {
    fn.apply(context, callbackArgs)
    clearTimeout(timeout)
    timeout = undefined
  }

  // Has to be a 'real' function, not an arrow function, to preserve
  // context of 'this'
  return function (...args: Array&lt;mixed>) {
    if (timeout) return

    callbackArgs = args
    timeout = setTimeout(callback, time)
  }
}
</code></pre>
        </article>
    </section>




</div>

<nav>
    <h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#compactArray">compactArray</a></li><li><a href="global.html#debounce">debounce</a></li><li><a href="global.html#first">first</a></li><li><a href="global.html#generateMarkup">generateMarkup</a></li><li><a href="global.html#immutablyDeleteProperty">immutablyDeleteProperty</a></li><li><a href="global.html#insertInArrIf">insertInArrIf</a></li><li><a href="global.html#insertInObjIf">insertInObjIf</a></li><li><a href="global.html#last">last</a></li><li><a href="global.html#objArrayToKeyedObj">objArrayToKeyedObj</a></li><li><a href="global.html#objToArray">objToArray</a></li><li><a href="global.html#partial">partial</a></li><li><a href="global.html#pipe">pipe</a></li><li><a href="global.html#range">range</a></li><li><a href="global.html#sortByProperty">sortByProperty</a></li><li><a href="global.html#throttle">throttle</a></li><li><a href="global.html#toggleClass">toggleClass</a></li></ul>
</nav>

<br class="clear">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Mon Jan 08 2018 08:55:44 GMT+0100 (CET)
</footer>

<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>