我正在尝试限制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.");
答案 0 :(得分:1)
下划线_.throttle
将返回您应该调用的新函数。与您使用notificationUSonce()
的方式相同。
现在你永远不会调用notificationError()
的实际限制版本。
var throttledFunction = _.throttle(notificationError, 1000, {trailing: false});
global.notificationErrorThrottled = function (title, body) {
global.notification_title = title;
global.notification_body = body;
throttledFunction();
}