gpt4 book ai didi

javascript - 为什么我的功能没有被限制?

转载 作者:行者123 更新时间:2023-12-03 07:47:13 26 4
gpt4 key购买 nike

我正在尝试限制 notificationError,每秒最多只调用一次。由于某种原因,它从未被调用,即使 notificationErrorThrottled 被调用。

var notificationError = function () {
console.log(`title: ${notification_title}; body: ${notification_body}`)
Notifications.error(notification_title, notification_body);
};

global.notificationErrorThrottled = function (title, body) {
global.notification_title = title;
global.notification_body = body;
_.throttle(notificationError, 1000, {trailing: false});
}

这是类似的有效代码(使用 _.once 而不是 _.throttle):

var notificationUS = function () {
Notifications.warn('US style?', "If you want to use moneylines, prefix them with '+' or '-'. Otherwise they are considered decimal odds.");
};

global.notificationUSonce = _.once(notificationUS);

这是我从另一个文件调用全局函数的方法:

notificationUSonce();
notificationErrorThrottled('Nope.', "Please check your input.");

最佳答案

下划线_.throttle将返回您应该调用的新函数。与使用 notificationUSonce() 的方式相同。
现在,您永远不会调用 notificationError() 的实际限制版本。

var throttledFunction = _.throttle(notificationError, 1000, {trailing: false});

global.notificationErrorThrottled = function (title, body) {
global.notification_title = title;
global.notification_body = body;
throttledFunction();
}

关于javascript - 为什么我的功能没有被限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35156485/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com