jashkenas/underscore

_.throttle()'d function is never called when frequency is high

JulesAU opened this issue · 3 comments

Test code (node.JS):

var underscore = require('underscore');

function y() {
    console.log('XXX');
}

var x = underscore.throttle(y, 100);

while (1) {
    x();
}

Expected: 'XXX' echoed every 100ms

Actual: XXX is echoed one time only.

In the inner loop in underscore.js, if (throttling) { is always true. So it seems to be a logic error rather than a problem with the event stack being saturated.

It seems to be use of debounce() which is preventing the throttling var from ever becoming false;

the while loop is blocking any async function from running, you can try replace the while loop with
setInterval(x,0);
which will allow the throttle function to perform normally.

Yes -- a hard while loop will certainly block the main JS thread. This isn't meaningful for our implementation of throttle.