Note that there are some explanatory texts on larger screens.

plurals
  1. PORewriting setInterval function using Underscore debounce
    primarykey
    data
    text
    <p>I'm rewriting my setInterval task so that I no longer have to clearInterval when I jump to a different tab on my site using Underscore.js debounce.</p> <p>I keep getting the error <code>_.debounce is not a function</code>.</p> <p>This is my original working code below. Basically, when the tab is 'pano', I run this function that changes the camera angle every 50ms. When I reclick on the 'pano' tab, clearInterval resets the timer.</p> <pre><code>clearInterval(timerID); if (target == 'pano') { initialize(); timerID = setInterval(function(){ if (!hovering &amp;&amp; !moved) { panoOptions.pov.heading += .5; panoOptions.pov.heading = wrapAngle(panoOptions.pov.heading); panorama.setOptions(panoOptions); } },50); initialize(); } </code></pre> <p>When I try to refactor my code to use <code>._debounce</code> (http://underscorejs.org/#debounce), I get an error and am not if my approach is correct.</p> <pre><code>timerID = _.debounce(function(){ if (!hovering &amp;&amp; !moved) { panoOptions.pov.heading += .5; panoOptions.pov.heading = wrapAngle(panoOptions.pov.heading); panorama.setOptions(panoOptions); } },50); $("#pano_toggle").click( function(){ console.log("CLICKED"); timerID(); } ); </code></pre> <p>I basically set up a click listener so that when the user clicks on the button, the debounce function starts running. I also added <code>console.log("CLICKED")</code> to see if my click listener is working, but it isn't picking up the click.</p> <p>Any tip on where my logic is failing?</p> <p>Edit:</p> <p>I made sure to require Underscore.js:</p> <pre><code>require(["require", "jquery", "underscore", "lib/jquery/jquery.galleria", "gmaps!", "//assets.pinterest.com/js/pinit.js", "twitter_bootstrap/bootstrap-dropdown"], function(require, _){ </code></pre>
    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.
 

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