<audio id="sound" autoplay="autoplay">
动态加入播放语音文件代码:
document.getElementById("sound").src="<%=basePath%>admin/media/global.wav"
2、动态弹出消息提示框:
在此处我导入了jquery.gritter.js和jquery.gritter.css,具体实现代码:
jQuery(document).ready(function() {
setInterval(function(){
$.post('ajax/linecheck',function(data){
var json=eval("("+data+")")
$.each(json,function(index,item){
$("#line"+item.id).html("")
$.each(item.localList,function(index,item2){
if(item2.attendOCList!=""){
$("#line"+item.id).append("<li class='in' id='in"+item2.id+"'><div class='avatar'>"
+item2.location+"</div><div class='message'><ul id=li"+item2.id+"></ul></div></li>")
}
$.each(item2.attendOCList,function(index,item3){
if(item3.status==0){
$("#li"+item2.id).append("<li class='user'><span class='username'>"+item3.person_name
+"</span><br><span style='color:red' class='username'>时间: "
+ item3.today+" "+item3.times +"</span><br><span class='username'>电话:"
+item3.person_phone+"</span><br><span class='username'>身份证:"
+item3.card_id+"</span></li><hr>")
}else{
$("#li"+item2.id).append("<li class='user'><span class='username'>"
+item3.person_name+"</span><br><span style='color:red' class='username'>时间: "
+ item3.today+" "+item3.times +"</span><br><span class='username'>电话:"
+item3.person_phone+"</span><br><span class='username'>身份证:"
+item3.card_id+"</span></li><hr>")
document.getElementById("sound").src="<%=basePath%>admin/media/global.wav"
setTimeout(function () {
var unique_id = $.gritter.add({
title: item3.person_name+"("+item2.location+")",
text:"<span class='username'>"+item3.person_name
+"</span><br><span style='color:red' class='username'>时间: "
+ item3.today+" "+item3.times +"</span><br><span class='username'>电话:"
+item3.person_phone+"</span><br><span class='username'>身份证:"+item3.card_id+"</span>",
sticky: true,
time: '',
class_name: 'my-sticky-class'
})
setTimeout(function () {
$.gritter.remove(unique_id, {
fade: true,
speed: 'slow'
})
}, 12000)
}, 2000)
}
})
})
这个东西要通过后端实现websocket类似的协议,再通过js的websocket接口来实现。
否则用比较笨的方法,打开新的页面时候建立一个计时器(setInterval),反复通过ajax轮询一个接口,接口得到某个值,ajax(fetch或者XMLRequest)取得这个值,然后把这个值插入到该插入的地方。最后清除计时器(clearInterval)