一个函数如何限制其调用?如果过于频繁,则不应丢弃这些调用,而是在时间上排队并间隔开,间隔为X毫秒.我看过油门和去抖动,但他们放弃了通话而不是将它们排队等待将来运行.
有没有比process()X毫秒间隔设置方法的队列更好的解决方案?JS框架中是否有这样的标准实现?到目前为止我看过underscore.js - 没什么.
在numpy中,我想检测信号从(先前已经)低于某个阈值的点到高于某个其他阈值.这适用于诸如去抖动或在存在噪声等情况下的准确过零点之类的事情.
像这样:
import numpy
# set up little test problem
N = 1000
values = numpy.sin(numpy.linspace(0, 20, N))
values += 0.4 * numpy.random.random(N) - 0.2
v_high = 0.3
v_low = -0.3
# find transitions from below v_low to above v_high
transitions = numpy.zeros_like(values, dtype=numpy.bool)
state = "high"
for i in range(N):
if values[i] > v_high:
# previous state was low, this is a low-to-high transition
if state == "low":
transitions[i] = True
state = "high"
if values[i] < v_low:
state …Run Code Online (Sandbox Code Playgroud) 我在使用下划线去抖的服务中有一个方法.
在该方法内部是对不同服务的方法的调用.我正在尝试测试调用不同的服务.
在我尝试测试debounced方法时,从不调用不同服务的方法,并且jasmine失败:
"期待的间谍aMethod被称为."
我知道它被调用(它以chrome格式登录到控制台),它只是在预期已经失败后被调用.
因此...(最好)不添加Sinon或其他依赖项并且
给予解决方案的奖励积分*不必将_.debounce转换为$ timeout ...
怎么办?
angular.module('derp', [])
.service('herp', function(){
return {
aMethod: function(){
console.log('called!');
return 'blown';
}
};
})
.service('Whoa', ['herp', function(herp){
function Whoa(){
var that = this;
this.mindStatus = 'meh';
this.getMind = _.debounce(function(){
that.mindStatus = herp.aMethod();
}, 300);
}
return Whoa;
}]);
Run Code Online (Sandbox Code Playgroud)
describe('Whoa', function(){
var $injector, whoa, herp;
beforeEach(function(){
module('derp');
inject(function(_$injector_){
var Whoa;
$injector = _$injector_;
Whoa = $injector.get('Whoa');
herp = $injector.get('herp');
whoa = new Whoa();
});
});
beforeEach(function(){
spyOn(herp, 'aMethod').andCallThrough();
});
it('has …Run Code Online (Sandbox Code Playgroud) 我对油门和去抖功能的概念有点不确定.
我们去掉了一个应该在某个事件发生后调用的函数.它用于拖动,键盘等事件,目的是不会一直触发事件被触发,而是在事件系列完成时触发.通常在键入整个单词后,或者拖动或调整大小序列已结束.
我们限制了在发生一系列事件时应该触发的函数,但是当我们想控制它被调用的次数时.就像在拖动运动中一样,我们希望仅在距离的每x个像素中调用该函数,或者仅每隔100ms调用该函数,而不是每次触发该事件时都调用该函数.因此,在发生一系列事件时调用油门功能,只需更少次数.
这是对这些功能及其目的的正确看法吗?还有其他功能可以区分它们吗?
如何在 python 中编写一个去抖装饰器,它不仅对调用的函数进行去抖,而且对所使用的函数参数/函数参数组合进行去抖?
去抖意味着在给定的时间范围内抑制对函数的调用,假设您在 1 秒内调用一个函数 100 次,但您只想允许该函数每 10 秒运行一次,去抖装饰函数将在 10 秒后运行该函数一次如果没有进行新的函数调用,则为最后一次函数调用。在这里我问的是如何使用特定的函数参数来消除函数调用的反跳。
一个例子是取消对人员对象的昂贵更新,例如:
@debounce(seconds=10)
def update_person(person_id):
# time consuming, expensive op
print('>>Updated person {}'.format(person_id))
Run Code Online (Sandbox Code Playgroud)
然后对函数进行去抖动 - 包括函数参数:
update_person(person_id=144)
update_person(person_id=144)
update_person(person_id=144)
>>Updated person 144
update_person(person_id=144)
update_person(person_id=355)
>>Updated person 144
>>Updated person 355
Run Code Online (Sandbox Code Playgroud)
因此,使用相同的 person_id 调用函数 update_person 将被抑制(去抖),直到 10 秒的去抖间隔过去,而没有对具有相同 person_id 的函数进行新的调用。
有一些去抖装饰器,但没有一个包含函数参数,例如: https: //gist.github.com/walkermatt/2871026
我通过函数和参数做了一个类似的油门装饰器:
def throttle(s, keep=60):
def decorate(f):
caller = {}
def wrapped(*args, **kwargs):
nonlocal caller
called_args = '{}'.format(*args)
t_ = time.time()
if caller.get(called_args, None) is None or t_ …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用组合 API 在 Vue 3 中创建一个可重用的去抖函数,但很难让它工作。
这是我到目前为止所拥有的:
去抖.js
const debounce = (fn, delay) => {
let timeout
return (...args) => {
if (timeout) {
clearTimeout(timeout)
}
timeout = setTimeout(() => {
fn(...args)
}, delay)
}
}
export default debounce
Run Code Online (Sandbox Code Playgroud)
基础组件.vue
<template>
<input type="text" @input="onInput" />
</template>
<script>
import debounce from './debounce'
export default {
setup() {
const onInput = () => {
debounce(() => {
console.log('debug')
}, 500)
}
return { onInput }
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
传递给去抖动函数的回调未被触发。我没有在控制台中看到输出。
我有一个现有的防抖实用程序,使用DispatchQueue. 它接受一个闭包并在达到时间阈值之前执行它。它可以这样使用:
let limiter = Debouncer(limit: 5)
var value = ""
func sendToServer() {
limiter.execute {
print("\(Date.now.timeIntervalSince1970): Fire! \(value)")
}
}
value.append("h")
sendToServer() // Waits until 5 seconds
value.append("e")
sendToServer() // Waits until 5 seconds
value.append("l")
sendToServer() // Waits until 5 seconds
value.append("l")
sendToServer() // Waits until 5 seconds
value.append("o")
sendToServer() // Waits until 5 seconds
print("\(Date.now.timeIntervalSince1970): Last operation called")
// 1635691696.482115: Last operation called
// 1635691701.859087: Fire! hello
Run Code Online (Sandbox Code Playgroud)
请注意,它不是Fire!多次调用,而是在最后一次使用上一个任务的值后仅 5 秒调用。该Debouncer实例配置为将队列中的最后一个任务保留 5 秒,无论调用多少次。闭包被传递到 …
我有以下用例:多个客户端推送到共享Redis列表.单独的工作进程应该排空此列表(处理和删除).Wait/multi-exec到位以确保,这很顺利.
出于性能原因,我不想立即调用'drain'进程,但是在x毫秒之后,从第一个客户端推送到(然后为空)列表的那一刻开始.
这类似于分布式下划线/ lodash 去抖动功能,计时器在第一个项目进入时开始运行(即:'领先'而不是'尾随')
我正在寻找以容错方式可靠地执行此操作的最佳方法.
目前我倾向于以下方法:
NX和px方法.这允许:
nx参数的用途px参数的用途1如果可以设置该值,则返回此命令,这意味着之前不存在任何值.0否则返回.A 1表示当前客户端是第一个运行该进程的客户端,因为Redis列表已耗尽.因此,这在纸上起作用,但感觉有点复杂.是否有其他方法可以以分布式容错方式工作?
顺便说一句:Redis和分布式队列已经到位,所以我不认为将它用于此问题是一个额外的负担.
const debounce = (func) => {
return (arg) => {
let timeoutId;
if (timeoutId){
clearTimeout(timeoutId);
}
timeoutId = setTimeout(() => {
func(arg);
}, 1000);
}
}
function hello(name){
console.log("hello " + name);
}
input.addEventListener("input", debounce(hello));
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我该如何去抖并hello使用 debounce 和 name 调用该函数"Brian"。
在代码行 2 上,return (arg) => {在变量中传递参数的代码是什么?
我知道它debounce(hello);调用了 debounce 函数,但是我该如何传递一个变量以便将其存储在中(arg)?
我有一个材质 UI 文本字段,其中包含一个onChange. 这onChange,就拿来event执行函数了handleOnChange。handleOnChange在当前的实现中,每次更改时都会执行该函数event。
是否可以debounce直接在 2000ms 后执行该函数event?
我的文本字段
<TextField
onChange={
event =>
handleOnChange(
event.target.value,
firstValue,
secondValue,
)
/>
Run Code Online (Sandbox Code Playgroud)
我的功能
const handleOnChange = (value, firstValue, secondValue) => {
...do something..
}
Run Code Online (Sandbox Code Playgroud)
我尝试了以下操作,但handleOnChange每次event更改时仍然会触发,而不是在 2000 毫秒后触发。
<TextField
onChange={
event =>
_.debounce(handleOnChange(
event.target.value,
firstValue,
secondValue,
), 2000)
/>
Run Code Online (Sandbox Code Playgroud) debouncing ×10
javascript ×6
python ×2
throttling ×2
angularjs ×1
arguments ×1
closures ×1
distributed ×1
function ×1
jasmine ×1
mutex ×1
numpy ×1
onchange ×1
python-3.x ×1
queue ×1
reactjs ×1
redis ×1
swift ×1
task-queue ×1
vue.js ×1
vuejs3 ×1