Underscore的油门功能不会触发

Rol*_*and 0 javascript underscore.js

_.throttle(function() {}, 250)功能是否仅触发click?因为我试图以一个小延迟运行一些代码,并且由于某种原因它似乎不起作用.

return _.throttle(function() {
    return ( $(this).hasClass('dataRevealed') ) ? $(this).addClass('animated fadeOut') : true;
}, 350);
Run Code Online (Sandbox Code Playgroud)

编辑:该函数看起来像这样:

Application.CardView.prototype.removeSimilarCards = function(_container) {
    return $(_container).find('[data-identifier="card-view"]').each(function() {
        console.log("first");
        _.throttle(function() {
            console.log("inner");
            return ( $(this).hasClass('dataRevealed') ) ? $(this).addClass('animated fadeOut') : true;
        }, 350);
    });
};
Run Code Online (Sandbox Code Playgroud)

shr*_*kuu 5

正如官方文档中提到的#throttle提到的那样,传入的函数必须是一个限制版本.你看,"限制"必须在传入之前执行.我只是让它工作.:) @closure在上面的评论中提到了这个.我们应该更多地阅读官方文件.

var throttled = _.throttle(updatePosition, 100);
$(window).scroll(throttled);
Run Code Online (Sandbox Code Playgroud)