.html()不会'页面更改或刷新时无法工作

.html() doesn't work when page changes or refreshes

本文关键字:刷新 工作 不会 html      更新时间:2023-09-26

我有一个JS,它在任何新通知到达时都会播放通知声音。当收到新通知时,它完全正常工作,应该播放声音,确实播放。

我所说的notification只是通过ajax调用从查询返回的integer。我通过脚本将这个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);