gpt4 book ai didi

javascript - window.location 不适用于带有 signalR 的服务器调用函数

转载 作者:行者123 更新时间:2023-11-30 13:06:39 25 4
gpt4 key购买 nike

这是我的代码:

<input type="button" id="SignOut" value="SignOut"  onclick="logout();" />

服务器端:

public void ondisconnected() // when the user is disconnected
{
try
{
DataRow[] UserRow = ConnectedClientDt.Select("ConnectionID='" + Context.ConnectionId + "'");
int ClientID = Convert.ToInt32(UserRow[0][0]);
new SQLHelper(SQLHelper.ConnectionStrings.WebSiteConnectionString).Update("Update clients set USER_STATUS='F' where CLIENT_ID=" + ClientID);
string query = " Select FRIEND_ID from friends where CLIENT_ID= " + ClientID;
DataTable FriendsDt = new SQLHelper(SQLHelper.ConnectionStrings.WebSiteConnectionString).getQueryResult(query);
for (int i = 0; i < FriendsDt.Rows.Count; i++)
{
DataRow[] FrRow = ConnectedClientDt.Select("ClientId=" + FriendsDt.Rows[i][0] + "");
if (FrRow.Length > 0)
Clients.Client(FrRow[0][2].ToString()).userDisconnected(ClientID);
}
DeleteUser(Context.ConnectionId);
}
catch (Exception ex)
{
return ;
}
}

客户端:

function logout() {
$.connection.chatHub.server.ondisconnected();
setTimeout(window.location= "LoginPage.aspx?action=logout", 5000);
}

当我按下按钮时,ondisconnected 函数没有执行,但是当我删除 settimeout 行时,代码正常工作,没有任何问题:s

最佳答案

下面这段代码将在评估时触发 window.location,这不是您想要的:

setTimeout(window.location= "LoginPage.aspx?action=logout", 5000);

你需要:

setTimeout(function(){window.location="LoginPage.aspx?action=logout"}, 5000);

通过将代码包装在一个函数中,它只会在函数被调用时执行,这将在 5 秒后发生。您之前的版本会立即评估 window.location,这意味着 signalr 没有时间与服务器通信。

关于javascript - window.location 不适用于带有 signalR 的服务器调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15448570/

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