I tested this out in 'real life' conditions using php and found that there were a couple of problems. The worst one was that IE and Opera were caching the update.php file so that the first update would be carried out but, not subsequent ones. I took care of that and some other minor problems as well as writing it all as a configurable script. You might want to set:
Code:
//Set Check Interval
var checkinterval=10;
//Set Check interval units - use 1 for seconds, 60 for minutes
var checkunit=1;
for testing purposes as it will check every ten seconds, the defaults I'm posting with are:
Code:
//Set Check Interval
var checkinterval=3;
//Set Check interval units - use 1 for seconds, 60 for minutes
var checkunit=60;
which checks every 3 minutes. Here is a demo page:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title>Update Array Script - Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
//The below array simulates an original array from a script
var pausecontent=new Array()
pausecontent[0]='<a href="http://www.javascriptkit.com">JavaScript Kit</a><br />Comprehensive JavaScript tutorials and over 400+ free scripts!'
pausecontent[1]='<a href="http://www.codingforums.com">Coding Forums</a><br />Web coding and development forums.'
pausecontent[2]='<a href="http://www.cssdrive.com" target="_new">CSS Drive</a><br />Categorized CSS gallery and examples.'
/*Update Array Script © John Davenport Scheuer
*as first seen in http://www.dynamicdrive.com/forums
*username:jscheuer1
*requires external updating file
*this notice must remain for legal use*/
//Set Array Name to update
var up_array=pausecontent;
//Set Check Interval
var checkinterval=3;
//Set Check interval units - use 1 for seconds, 60 for minutes
var checkunit=60;
//Set path (optional - if required or desired) and source (required) of external file containing the update_up_array() function
var update_source='update.php'; //comment - use the .asp, .php or whatever extension here as appropriate
////////////Stop Editing//////////////
function attach_update(testing_it){
if (!testing_it) {
var newone=1, dt=new Date().getTime();
var hd=document.getElementsByTagName('head')[0];
var scripts=hd.getElementsByTagName('script');
var upd=document.createElement('script');
upd.type='text/javascript';
upd.src=update_source+'?ud='+dt;
for (var i_tem = 0; i_tem < scripts.length; i_tem++)
if (scripts[i_tem].src.indexOf(update_source)>-1){
hd.replaceChild(upd,scripts[i_tem]);
newone=0;
break;
}
if(newone)
hd.appendChild(upd);
}
if(typeof update_up_array=='function'){
update_up_array ();
update_up_array=null;
}
else
setTimeout("attach_update('t')", 100);
}
setInterval("attach_update()", checkinterval*checkunit*1000)
</script>
</head>
<body>
<input type="button" value="see what's up" onclick="alert('pausecontent\'s length='+pausecontent.length+'\npausecontent\'s last item='+pausecontent[pausecontent.length-1]+'\nnumber of scripts in the head='+document.getElementsByTagName('head')[0].getElementsByTagName('script').length)">
</body>
</html>
and here is a demo update.php file:
PHP Code:
/*possible update.php contents - good for testing purposes*/
function update_up_array () {
up_array.length=0;
up_array[0]='bob'
up_array[1]='<? print date("F d, Y H:i:s", time())?>'
}
I did not test this with the actual script involved. I will leave that up to you. If it initializes off of the original array and then never checks its length or contents, there will probably be problems but, I don't think it does. Other problems may arise. I will also leave up to you formulating your own optimal update.php file. It must be a function though, not merely an array. If you do have trouble with it, let me know. Make sure the test version is working first.
Bookmarks