My idea was to make a HTML page that would change its background every 3 seconds or so. I started out simple, with just two possible variations and was intending to move on to more possible background images later. For example, every time 3 seconds pass, some global temp variable (if global scope exists in JS or HTML at all, like in C++) counting number of times the background changed would increase for 1, which would, in the end, determine what the next background would be.
For testing and learning purposes, I started out with two backgrounds determined by if condition:
<script>
function diffBg (){
var loc="url('1.jpg')";
var loc2="url('2.jpg')";
if (document.body.style.backgroundImage == loc)
{
document.body.style.backgroundImage = loc2;
}
else
{
document.body.style.backgroundImage = loc;
}
}
</script>
The function would be triggered by body event:
<body onload="setInterval(diffBg,3000)">
When loaded, page background is at first white. After 3 seconds, the background changes, never to be changed again.
I assume the error has to do with body loading only once, thus the function is being called only once. How could I fix this?
I'm a JS and HTML beginner. I've only programmed in C++ and I guess I use my "C++ logic" with everything I do.
Every help is greatly appreciated. Thanks in advance.
Edit: Post What's the easiest way to call a function every 5 seconds in jQuery? does not help me at all. I tried mentioned methods and none of them worked.