[英].html() doesn't work when page changes or refreshes
我有一個 JS,它在任何新通知到達時播放通知聲音。 當新通知出現時,它完全正常工作,它應該播放聲音並且確實播放。
我正在談論的notification
只是一個integer
,它是通過 ajax 調用從查詢返回的。 我通過腳本將此integer
設置到我的<asp:label.../>
。
問題是:我已經在MasterPage
上編寫了腳本。 因此,每次我打開一個新頁面或刷新同一頁面時, <asp:Label.../>
都會被清除,這是我使用.html(value)
從我的腳本中設置的,導致腳本每次都再次運行聲音頁面刷新或加載另一個頁面。
問題可能是,該值不是持久的?
我希望該值應該設置為標簽的 html,並且它的值應該在所有頁面上都是持久的。 對於這種persistence
我該怎么辦?
我的腳本是:
myFunction();
function myFunction() {
$.get("AjaxServicesNoty.aspx", function (data) {
var recievedCount = parseInt(data);
alert(recievedCount);
var existingCount = $(".lblEventCount").text();
if (existingCount == "") {
existingCount = 0;
alert(existingCount);
} else {
existingCount = parseInt($(".lblEventCount").text());
alert(existingCount);
}
// if (existingCount == "" && recievedCount != 0) {
// $(".lblEventCount").html(recievedCount);
// $(".lblAcceptedCount").html(recievedCount);
// var sound = new Audio("Sound/notificationSound.wav");
// sound.play();
// }
if ((parseInt(recievedCount) > parseInt(existingCount)) && existingCount == 0) {
$(".lblEventCount").html(recievedCount);
$(".lblAcceptedCount").html(recievedCount);
var sound = new Audio("Sound/notificationSound.wav");
sound.play();
} else {
$(".lblEventCount").html(existingCount);
$(".lblAcceptedCount").html(existingCount);
}
});
}
setInterval(myFunction, 5000);
使用 DIV ID 而不是通過類選擇
$("#divID").html(existingCount);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.