Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using your example, why not pass toggle in as arg 1... something like:</p> <pre><code>var toggle = true; var debouncedFunk = function(toggle) { if (toggle) // the function call else // something else }; debounce(debouncedFunk, toggle, 1250); </code></pre> <p>You should also look into using the Function objects <code>.call</code> and <code>.apply</code> methods. They are for calling the function and passing in arguments. Taking the example function:</p> <pre><code>var example = function(one, two) { // Logic here }; </code></pre> <p>You can call it in three ways:</p> <pre><code>// First example(1, 2); // Second example.call({}, 1, 2); // Third example.apply({}, [ 1, 2 ]); </code></pre> <p>The first is the standard way to call a function. The difference between the first and the <code>.call</code> is that the first parameter to <code>.call</code> is the context object of the function (what <code>this</code> will point to inside the function), the other parameters are passed after that (and a known list is required for <code>.call</code>. The benefit of <code>.apply</code> is that you can pass an array to the function of arguments and they will be assigned to the parameter list appropriately, the first parameter is still the context object.</p> <p>It would simplify your debounce function, instead of having to deal with a structured object as you currently do.</p> <p>A suggestion for your debounce:</p> <pre><code>var debounce = function(funk, delay) { var args = []; if (arguments.length &gt; 2) args = [].slice.call(arguments, 2); setTimeout(function() { funk.apply({}, args); }, delay); }; </code></pre> <p>Changing your current if to:</p> <pre><code>var toggle = true; var debouncedFunk = function(toggle) { if (toggle) // Do if true else // DO if false }; debounce(debouncedFunk, 1000, toggle); </code></pre> <p>Maybe too much information (sorry)?</p> <p>As a last note, I'd recommend using a framework (if possible) where these functions have been implemented already (and many other useful functions) such as <a href="http://underscorejs.org/" rel="nofollow noreferrer">Underscore</a>. Using Underscore your example would look like:</p> <pre><code>// Define debouncedFunk and toggle debouncedFunk = _.bind(debouncedFunk, {}, toggle); debouncedFunk = _.debounce(debouncedFunk, 1000); debouncedFunk(); </code></pre> <p><strong>EDIT</strong></p> <p>Fixed the underscore example, <code>_.debounce</code> returns a function that will execute only after the delay but it still needs to be called.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload