我正在嘗試在我的webapp中創建一個停止按鈕。 webapp爲不同的文件創建批量快捷方式。我曾嘗試使用$.connection.shortcutHub.stop()
但是,這出現了一個錯誤說Cannot read property 'shortcutHub' of undefined(anonymous function)
Signalr關閉連接
代碼如下。一旦單擊stop
按鈕,我需要停止連接。停止按鈕的ID是stopButton
。
$(document).ready(function() {
// initialize the connection to the server
var progressNotifier = $.connection.shortcutHub;
// client-side sendMessage function that will be called from the server-side
progressNotifier.client.sendMessage = function (message, percent) {
// update progress
UpdateMessage(message, percent);
};
progressNotifier.client.redo = function() {
redo();
};
progressNotifier.client.success = function() {
success();
};
progressNotifier.client.fail = function() {
fail();
};
// establish the connection to the server and start server-side operation
$.connection.hub.start().done(function() {
$('#confirmbutton').click(function() {
jQuery.noConflict();
document.getElementById('closeButton').setAttribute("class", "btn btn-default hidden");
$('#myModal').modal('show');
//document.getElementById('confirmbutton').disabled = true;
//document.getElementById('barcodepanel').setAttribute("class", "panel panel-default");
var ticket = getCookie('ticket');
var path = getCookie('CBSShortcut_Path');
var checkeddocs = getCheckedBoxes("dcheck");
var checkedfolders = getCheckedBoxes("fcheck");
progressNotifier.server.createshortcuts(ticket, path, checkeddocs, checkedfolders);
});
$('#stopButton').click(function() {
document.getElementById('closeButton').setAttribute("class", "btn btn-default");
document.getElementById('confirmbutton').disabled = false;
//What do I put here?
});
});
function UpdateMessage(message, percent) {
// get result div
var msg = $("#result");
// set message
msg.html(message);
//set value of progress bar
document.getElementById('closeButton').setAttribute("class", "btn btn-default hidden")
$('#progressbar').css('width', percent + '%').attr('aria-valuenow', percent);
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
}
return "";
}
function redo() {
document.getElementById('confirmbutton').disabled = false;
jQuery.noConflict();
$('#myModal').modal('hide');
}
// Pass the checkbox name to the function
function getCheckedBoxes(chkboxclass) {
var checkboxes = document.getElementsByClassName(chkboxclass);
var checkboxesChecked = [];
var ids = "";
// loop over them all
for (var i = 0; i < checkboxes.length; i++) {
// And stick the checked ones onto an array...
if (checkboxes[i].checked) {
checkboxesChecked.push(checkboxes[i]);
ids = ids + checkboxes[i].getAttribute("Name") + ",";
}
}
// Return the array if it is non-empty, or null
//return checkboxesChecked.length > 0 ? checkboxesChecked : null;
return ids;
}
}
);`
任何幫助表示讚賞。我已經嘗試了一切,谷歌已經拋出我的方式(這主要是stackoverflow網站),我仍然有同樣的問題。
我會在哪裏把這個?我在一些地方嘗試過,但是我一直得到相同的錯誤。 – JohZant
好的。當你開始連接時,把$ .connection放在一個變量中,稍後使用該變量來像這樣停下來 - var ChatConnection = $。connection;當你想停止使用ChatConnection.stop();注意: - 保持變量不變。 –
我已將'progressNotifier'分配爲保存連接的全局變量。當我輸入'progressNotifier.stop();'它告訴我,它不是一個函數。 – JohZant