javascript - 无法从 signalR 回调中调用 javascript 函数
问题描述
我有一个包含 javascript 的 html 页面。在这个页面中,我调用了 3 个 ajax 调用并等待它们全部完成。
我正在使用 signalR 并尝试从 onMessageReceivedSyncTasks() 函数更新 UI。如果我在这里调用一个 jquery 选择器,它工作正常。但是..如果我尝试调用函数 IntitalLoad() 我得到一个错误。都在同一个范围内吗?
设置.html
<html>
<body>
<!-- html code not included -->
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
$(document).ready(function () {
function InitialLoad() {
$.when(GetSettings(), GetTimebands(), GetApnList()).done(function (a1, a2, a3) {
// UI changes when all 3 ajax functions done
});
}
function GetSettings() {
return $.ajax({
// ajax call 1
});
}
function GetTimebands() {
return $.ajax({
// ajax call 2
});
}
function GetApnList() {
return $.ajax({
// ajax call 3
});
}
});
<script src="/signalr/dist/browser/signalr.js"></script> <-- microsoft signalr lib -->
<script src="/signalr/dist/browser/messagebroker.js"></script>
</body>
</script>
</body>
</html>
消息代理.js
signalrConnection.on("onMessageReceivedSyncTasks", function (eventMessage) {
if ($('#table_buttons').length) {
$('#table_buttons').DataTable().ajax.reload(); // This selector works fine
}
InitialLoad(); // Calling a function returns error
});
错误:
[2021-11-05T13:21:33.643Z] Error: A callback for the method onmessagereceivedsynctasks threw error 'ReferenceError: InitialLoad is not defined'.
解决方案
推荐阅读
- python - 调试 python django 项目
- c - 尽管有足够的可用内存 (32 GB),但在 malloc() 处理 12 GB 内存后收到“分段错误:11”
- php - Facebook OAuth 一次又一次地请求许可
- bash - 编写一个 shell 脚本,该脚本将返回您的存储库 git 跳过的文件列表
- embedded - STM32 上的 Eigen 只能工作到一定大小
- matlab - griddata 在所需区域之外生成数据
- javascript - 即使有结果,Promise 也会返回未定义的值
- android - 进程'命令'NDK PATH'以非零退出值2完成
- c++ - 计算字符串 S 中所有数字出现的次数。?
- sql - 在sql表中查找重复的列值对