douxian4323 2014-10-25 19:12
浏览 104

使用ajax的jQuery通知声音

I have a php file that returns new items from db and using ajax, i update a div with number of new items.

In order to make it look a bit like facebook, i have inside a setInterval(3000 ms) the ajax call.All ok here. Now i want to play a sound when new items comes up.

My code looks like this:

$.ajax({
  type: "POST",
  url: "script.php",
    data: { action : somedata },
    cache: false,
    success: function(html){
      if(html > 0) {
        // update div
      }else{
        //do nothing
      }
    }
})`

I googled how to play a sound in jQuery, and i got a working solution:

var audioElement = document.createElement('audio');
audioElement.setAttribute('src', 'notification.ogg');
audioElement.setAttribute('autoplay', 'autoplay');

$.get();
audioElement.addEventListener("load", function() {
audioElement.play();
}, true);

If i put the previous code here:

if(html > 0) {
here
}

the sound is played, but there's a problem. It's played every 3000ms. I don't want this. I want to play it only when a new item comes up in the db, and not every time the ajax call is made.

How can i achieve this?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题