JS,用于播放提示音以通知未按预期工作
JS for playing beep sound for notification not working as expected
我有一个脚本,它执行ajax调用来接收来自某个页面的通知。回调函数中接收的数据是整数(0,1,2,3…),仅为查询中选择的行数。请在下面的描述中找到我正在使用的查询。
我正在使用以下JS:
function myFunction() {
$.get("AjaxServicesNoty.aspx", function (data) {
var recievedCount = data;
var existingCount = $("lblEventCount").text();
if (existingCount == "") {
$(".lblEventCount").html(recievedCount);
$(".lblAcceptedCount").html(recievedCount);
var sound = new Audio("Sound/notificatonSound.wav"); // buffers automatically when created
sound.play();
}
else if (parseInt(recievedCount) > parseInt(existingCount)) {
$(".lblEventCount").html(recievedCount);
$(".lblAcceptedCount").html(recievedCount);
var sound = new Audio("Sound/notificatonSound.wav");
sound.play();
}
else {
$(".lblEventCount").html(existingCount);
$(".lblAcceptedCount").html(existingCount);
// var sound = new Audio("Sound/notificatonSound.wav");
// sound.play();
}
});
}
setInterval(myFunction, 3000);
声音播放良好现在。现在,当调用setInterval()
函数时,即使根据我的if-else
条件,每三秒也会持续播放一次嘟嘟声。它不应该这么玩。只有在有新更新的情况下才能播放。
我使用了以下查询:
public int getAcceptedCount(int CustomerID)
{
string query = @"SELECT Status
FROM tblEventService
WHERE EventID IN (SELECT EventID FROM tblEvent WHERE (CustomerID = @cid)) AND (Status = 'Accepted' OR Status = 'Rejected')";
List<SqlParameter> lstP = new List<SqlParameter>();
lstP.Add(new SqlParameter("@cid", CustomerID));
DataTable dt = DBUtility.SelectData(query, lstP);
if (dt.Rows.Count > 0)
{
return dt.Rows.Count;
}
else
{
return 0;
}
}
查询选择Status
为Accepted or Rejected
的所有行。因此,如果有任何新的Accepted or Rejected
事件,则计数将增加。因此JS应该播放通知声音!
我在这里做错了什么?
我认为您的代码的问题在于这个if语句if (existingCount == "")
。如果你的exisitingCount
应该是一个整数,你不必测试它,否则每当你的exisitingCount
等于0
时,你的声音就会被播放。
但是,如果有时你可以得到一个空字符串,那么你可以优化你的代码并删除第一个条件,并在开始测试之前解析所有数据:
function myFunction() {
$.get("AjaxServicesNoty.aspx", function (data) {
var recievedCount = parseInt(data);
var existingCount = parseInt($("lblEventCount").text());
if ( recievedCount > existingCount ) {
$(".lblEventCount").html(recievedCount);
$(".lblAcceptedCount").html(recievedCount);
var sound = new Audio("Sound/notificatonSound.wav");
sound.play();
} else {
$(".lblEventCount").html(existingCount);
$(".lblAcceptedCount").html(existingCount);
}
});
}
setInterval(myFunction, 3000);
浏览器正在http://localhost:40825/EventManagement/Sound/notificationSound.wav.听起来路径是错误的,你在JS中引用它的方式是在JS文件所在的文件夹中寻找一个名为Sound的文件夹(EventManagement)。
这个声音文件夹到底在你的机器上的什么位置?如果它是EventManagement文件夹的同级文件夹,请尝试new Audio("../Sound/notificatonSound.wav");
。否则,我只会将该文件夹和音频移到EventManagement文件夹中:)
- JS,用于播放提示音以通知未按预期工作
- 如果谷歌工作表上的指定列发生更改,则发送电子邮件通知
- Firebase推送通知-节点工作程序
- 推送通知是如何工作的
- 如何将数据(如用户 ID)传递给 Web 工作人员,以便从服务器获取其他推送通知数据
- HTML5通知与服务工作者在Chrome移动不工作
- 是否有任何工作的Cordova推送通知
- 重定向在本地通知点击离子不工作
- IOS本地通知不能与PhoneGap一起工作
- jQuery延期.通知不工作
- JavaScript通知在函数中没有按预期工作
- cordova 3.5.0的本地通知不工作
- 如何通过angularjs服务让剑道通知工作
- HTML5通知不工作在移动Chrome
- Stack Overflow桌面通知是如何工作的
- 户外电子邮件通知与附件在不工作
- Nativescript可以't使android通知操作正常工作
- 我有ngCordova本地通知工作,如何让它为每个用户触发
- 使用canvas创建桌面通知图像,只有一个图像可以从后台页面工作
- 通知系统的工作原理