I was inspired by this thread: Refresh image with a new one at the same url
I'm trying to refresh a WordPress header background image every 5 seconds.
The background image is set in styles.css: background-image: url(/some/path/rotator.php);
"Rotator.php" is PHP script that chooses a random image, and its working nicely. The path doesn't change.
I was hoping the following javascript would refresh it:
var newImage = new Image();
newImage.src = "/some/path/rotator.php";
function updateImage()
{
if(newImage.complete) {
document.getElementById("header").src = newImage.src;
newImage = new Image();
number++;
newImage.src = "/some/path/rotator.php?" + new Date().getTime();
}
setTimeout(updateImage, 5000);
}
The script is loaded by the following PHP in Functions.php:
<?php
add_action('after_setup_theme', 'load_mbr_scripts');
function load_mbr_scripts() {
wp_register_script('updateImage', get_stylesheet_directory_uri().'/refreshheader.js', array('jquery'), '1.7', true );
wp_enqueue_script( 'updateImage', get_stylesheet_directory_uri().'/refreshheader.js', array( 'jquery' ) );
}
add_action('wp_enqueue_scripts', 'load_mbr_scripts');
?>
Firebug shows my javascript being loaded. It doesn't seem to be refreshing anything, however.
Am I specifying the header element to refresh incorrectly? Or am I doing something else wrong?
I have fair knowledge of HTML and CSS, and a little PHP, but I am a total Javascript N00b, so any advice will be appreciated!