为什么我的功能没有受到限制?

Why isn't my function throttled?

我正在尝试限制 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 将 return 一个您应该调用的新函数。与使用 notificationUSonce().
的方式相同 现在你永远不会调用 notificationError().

的实际节流版本
var throttledFunction = _.throttle(notificationError, 1000, {trailing: false});

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