我有一个JS在任何新通知到达时播放通知声音。它的工作原理完全正常,当一个新通知到来时,它应该播放声音,它确实发挥。.html()在页面更改或刷新时不起作用
notification
我在说的只是一个integer
,它是通过ajax调用从查询返回的。我通过脚本将此integer
设置为我的<asp:label.../>
。
的问题是:我已经写上MasterPage
脚本。因此,每当我打开新页面或刷新同一个页面时,<asp:Label.../>
被清除,我通过脚本使用.html(value)
来设置,导致脚本在每次页面刷新或其他页面加载时再次运行声音。
问题可能是,该值不是持续?
我希望值应该被设置为标签的html,并且其值也应该在所有页面上保留。我应该怎么做这个persistence
?
我的脚本是:通过类选择
<script type="text/javascript">
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);
</script>
我不明白,很好,但在客户端,如果你刷新asp.net页面或从服务器重新加载页面,这些值将不会持久。你尝试过使用cookie吗? – Serendipity
再次我不能访问来自JS的会话和cookie值?我可以吗 ? –
我刚刚看过,我可以从JS访问cookies!无论如何,我会找到解决办法!谢谢! –