简体   繁体   English

.html() 在页面更改或刷新时不起作用

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

I have a JS that plays a notification sound on the arrival of any new notifications.我有一个 JS,它在任何新通知到达时播放通知声音。 It works completely fine when a new notification comes, it should play the sound and it does play.当新通知出现时,它完全正常工作,它应该播放声音并且确实播放。

The notification I am talking about is only an integer which is returned from a query through an ajax call.我正在谈论的notification只是一个integer ,它是通过 ajax 调用从查询返回的。 I set this integer into my <asp:label.../> through the script.我通过脚本将此integer设置到我的<asp:label.../>

The problem is : I have written the script on MasterPage .问题是:我已经在MasterPage上编写了脚本。 So every time I open a new page, or a refresh the same one the <asp:Label.../> gets cleared which I set from my script using .html(value) causing the script to run the sound again as every time the page refreshes or another page is loaded.因此,每次我打开一个新页面或刷新同一页面时, <asp:Label.../>都会被清除,这是我使用.html(value)从我的脚本中设置的,导致脚本每次都再次运行声音页面刷新或加载另一个页面。

The problem may be is , that the value is not persistent ?问题可能是,该值不是持久的

I want that the value should be set to the html of the label and also its value should be persistent on all the pages .我希望该值应该设置为标签的 html,并且它的值应该在所有页面上都是持久的。 What should I do for this persistence ?对于这种persistence我该怎么办?

My Script is :我的脚本是:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM