_.debounce做什么? [英] What does _.debounce do?

查看:210
本文介绍了_.debounce做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用的项目使用_.debounce()。



用于去抖动的Underscore JS文档如下所示:


去抖动 code> _。debounce(function,wait,[immediate])



创建并返回已传递函数的新debounced版本
将延迟其执行,直到自上次调用以来等待毫秒以后,
已经过去了。


这显然是假设任何人想知道去抖动的意思,已经知道什么是抖动手段。 / p>

debounce实际上做了什么?

解决方案

基本上它阻止了电话如果它在短时间内被多次呼叫,只会有一个实例被调用。



为什么要使用它?



像window.onresize这样的事件可以快速连续多次发火。如果您需要对新职位进行大量计算,则不想多次触发计算。您只想在用户完成调整大小事件时触发它。


A project I've been working on uses _.debounce().

The Underscore JS documentation for debounce reads as follows:

debounce _.debounce(function, wait, [immediate])

Creates and returns a new debounced version of the passed function that will postpone its execution until after wait milliseconds have elapsed since the last time it was invoked.

This obviously assumes that anyone who wants to know what debounce means, already knows what debounce means.

What does debounce actually do?

解决方案

Basically it throttles calls so if it is called more than once in a short period of time, only one instance will be called.

Why would you use it?

Events like window.onresize fire multiple times in rapid succession. If you need to do a lot of calculations on the new position, you would not want to fire the calculations multiple times. You only want to fire it when the user has finished the resizing event.

这篇关于_.debounce做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆