我在桌面上有一个带有SQL查询的页面,可以在大屏幕上显示结果.
然后我浏览到index.php,其中包含以下代码:
// <![CDATA[
$(document).ready(function() {
// This part addresses an IE bug. without it, IE will only load the first number and will never refresh
$.ajaxSetup({ cache: false });
setInterval(function() {
$('.container').load('data.php');
}, 2000); // the "2000" here refers to the time to refresh the div. it is in milliseconds.
});
// ]]>
Run Code Online (Sandbox Code Playgroud)
HTML:
<div class="container"><h3>Loading Data...</h3></div>
Run Code Online (Sandbox Code Playgroud)
所以它不断加载这个页面.
我想做的就是拥有它,如果任何查询包含需要对其执行操作的数据,表格单元格将闪烁2种颜色,并且每隔5分钟将播放一次声音.
什么是最好的方法来保持页面不断加载?
我会将 .load() 更改为 ajax 调用,该调用在完成后调用一个函数。检查下面的脚本:
// Prepare the audio - replace the link with your own mp3
var audioElement = document.createElement('audio');
audioElement.setAttribute('src', 'http://www.uscis.gov/files/nativedocuments/Track%2090.mp3');
// Global that will control the flashing / playing of sound
var alertUser = false;
$(document).ready(function() {
$.ajaxSetup({ cache: false }); // Fix IE bug
setInterval(function(){
$.ajax({
url: "data.php",
complete: function( jq, content ){
$('.container').html( jq.response );
if( jq.response.indexOf( 'from' ) != -1 ) { // your logic goes here to decide the warning
alertUser = true;
} else {
alertUser = false;
}
}
});
}, 2000);
setInterval(function(){
if( alertUser ) {
// play tune
audioElement.play();
// flash background
window.setTimeout( function(){
$('.container').css('background-color','red')
}, 200 );
window.setTimeout( function(){
$('.container').css('background-color','blue')
}, 400 );
window.setTimeout( function(){
$('.container').css('background-color','transparent')
}, 600 );
}
}, 1000);
});
Run Code Online (Sandbox Code Playgroud)