簡體   English   中英

為什么我的功能沒有節制?

[英]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將返回您應調用的新函數。 就像使用notificationUSonce()
現在,您永遠不會調用notificationError()的實際限制版本。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM