用于播放提示音的 JS 无法按预期工作
JS for playing beep sound for notification not working as expected
我有一个脚本,它执行 ajax 调用以接收来自某些 page.The 回调函数中收到的数据的通知是一个 整数(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 应该播放通知声音!
我做错了什么?
浏览器正在 http://localhost:40825/EventManagement/Sound/notificationSound.wav 寻找声音文件。听起来路径是错误的,您在 JS 中引用它的方式是在 JS 文件所在的文件夹 (EventManagement) 中查找名为 Sound 的文件夹。
这个 Sound 文件夹在您机器上的具体位置是什么?如果它是 EventManagement 文件夹的同级文件夹,请尝试 new Audio("../Sound/notificatonSound.wav");
。否则,我只是将该文件夹和音频移动到 EventManagement 文件夹中:)
我认为您的代码中的问题是这个 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);
我有一个脚本,它执行 ajax 调用以接收来自某些 page.The 回调函数中收到的数据的通知是一个 整数(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 应该播放通知声音!
我做错了什么?
浏览器正在 http://localhost:40825/EventManagement/Sound/notificationSound.wav 寻找声音文件。听起来路径是错误的,您在 JS 中引用它的方式是在 JS 文件所在的文件夹 (EventManagement) 中查找名为 Sound 的文件夹。
这个 Sound 文件夹在您机器上的具体位置是什么?如果它是 EventManagement 文件夹的同级文件夹,请尝试 new Audio("../Sound/notificatonSound.wav");
。否则,我只是将该文件夹和音频移动到 EventManagement 文件夹中:)
我认为您的代码中的问题是这个 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);