JS,用于播放提示音以通知未按预期工作

JS for playing beep sound for notification not working as expected

本文关键字:工作 通知 用于 播放 提示 JS      更新时间:2023-09-26

我有一个脚本,它执行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;
            }
        }

查询选择StatusAccepted 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文件夹中:)