【问题标题】:signalr async response javascript callback信号器异步响应javascript回调
【发布时间】:2017-08-22 14:01:49
【问题描述】:

请您建议将我的响应从 signalR 集线器传递给具有回调的函数调用的最佳方法。我尝试使用全局变量来存储回调并在接收数据时调用它,但没有运气.这就是我喜欢做的事情;

school.lib.setmytime = function(_time){

 console.log('mytime is :' + _time);
}

school.lib.gettime = data.getservertime(school.lib.setmytime);

var myhub;

data.connection = function(){
    $connection.hub.url = 'http://myserver.com:65442';
    $connection.hub.start();

    myhub = $.connection.adminHub;

    myhub.client.getServerTime = function (_time){
         //How do i access the callback 
         //function in data.getservertime so as to return time to the 
        //school.lib.gettime?
    }

}

data.getservertime = function(callbak){

    myhub.server.getServerTime();

}

【问题讨论】:

    标签: javascript jquery signalr.client


    【解决方案1】:

    鉴于您当前的设计,您应该将回调传递给 getServerTime 方法:

    myhub.client.getServerTime = function (callback){
         //How do i access the callback 
         //function in data.getservertime so as to return time to the 
        //school.lib.gettime?
    
        // after you have retrieved the time
        if (typeof callback === 'function') {
           callback(school.lib.gettime);
        }
    }
    
    ...
    
    data.getservertime = function(callback){
       myhub.server.getServerTime(callback);
    }
    

    【讨论】:

    • 如何从我的服务器调用回调函数?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 2022-01-19
    相关资源
    最近更新 更多