gpt4 book ai didi

javascript - signalr 异步响应 javascript 回调

转载 作者:行者123 更新时间:2023-11-28 04:16:13 25 4
gpt4 key购买 nike

请您建议将我的响应从 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();

}

最佳答案

鉴于您当前的设计,您似乎应该将回调传递给 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);
}

关于javascript - signalr 异步响应 javascript 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45819811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com