节流阀&反弹功能 [英] Throttle & debounce functions

查看:113
本文介绍了节流阀&反弹功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我明白了:



我们的debounce 函数应该在某个事件发生后调用。它用于拖拽,键入等事件,目的是在事件被触发时不会触发,而是在一系列事件完成时触发。通常在整个单词被输入后,或拖动或调整大小序列结束。



我们节气门 a函数应该在发生一系列事件时触发,但是当我们想要控制它被调用的次数时。就像在拖动中一样,我们只需要每隔x个像素距离调用函数,或者每隔100ms调用一次函数,而不是每次触发事件。因此,在一系列事件发生时调用油门函数,只需要更少次数。



问题:



这是对这些功能和目的的正确认识吗?是否还有其他功能可以区分它们?

解决方案

是的,这是一个很好的差异摘要。



但是,您可能需要强调的是,这些方法实际上并不会改变它们被调用的功能。他们只是创建一个新的函数(带有限制速度行为所绑定的身份),并且可以根据需要经常调用该函数,并将调用内部中继到去抖或扼杀函数。


I am a bit uncertain with the concepts of throttle and debounce functions.

As I get it:

we debounce a function that should be called after a certain event has happened. It is used in events like drag, keyup, etc. with the purpose of not firing all the time the event gets fired but instead when the series of events is done. Typically after a whole word has been typed, or a drag or resize sequence has ended.

we throttle a function that should fire while a series of events is happening, but when we want to control the amount of times it gets called. Like in a drag movement we want the function to be called only every x pixels of distance, or only every 100ms, and not every time the event is fired. So the throttle function is called while the series of events is happening, just fewer times.

Question:

is this a correct perception of what these functions are and their purpose? are there other features that distinguish them?

解决方案

Yes, that's a good synopsis of the differences.

However, you might want to emphasize that these methods don't actually alter the functions they are called upon. They just create a new function (with an identity, to which the rate limiting behaviour is bound) that can be called as often as necessary, and internally relays the calls to the debounced or throttled function.

这篇关于节流阀&反弹功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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