【问题标题】:pusher unbind not working推杆解绑不工作
【发布时间】:2017-09-20 20:03:04
【问题描述】:

我正在使用带有 PHP 后端的 javascript pusher library (angularjs)。

js:

// Enable pusher logging - don't include this in production
Pusher.logToConsole = true;

var pusher = new Pusher('API_KEY', {
        cluster: 'ap2',
        encrypted: true
});
var channel = pusher.subscribe('my-channel');
channel.bind('my-event', function (data) {
   common.flashMsg('success', data.message);
});

绑定和解绑代码:

$scope.workerStatus = function (status) {
        if (status == 'available') {
            common.flashMsg('success', "Welcome Again...!");
            channel.bind('my-event', function (data) {
                common.flashMsg('success', data.message);
            });
        } else {
            common.flashMsg("error", "You're now offline. Good Bye.");
            channel.unbind('my-event', function () {
                common.flashMsg('success', data.message);
            });
        }
}

我想取消绑定channel,当用户注销并停止通知,但频道unbind 事件不起作用时,用户仍然能够收到通知。 我错过了什么?

【问题讨论】:

  • 我传入了对不同函数的引用,所以调用失败,现在问题解决了,在 unbind 的第二个参数中使用相同的函数

标签: javascript angularjs pusher


【解决方案1】:

我认为您遇到问题的原因是您将函数传递给 unbind 调用。尝试省略第二个参数以删除该事件的所有侦听器。如果您有多个事件处理程序,则保留传递事件处理程序以取消绑定该特定处理程序。如果您将多个事件处理程序绑定到事件并且只想取消绑定其中一个,这将非常有用。

在此处查看更多信息:https://github.com/pusher/pusher-js#bind-and-unbind

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-27
    • 2018-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多