I'm trying to learn javascript, buy now when I try to repeat a function, it won't seem to work.
This is my function:
function heyhey(el){
el.style.position = "absolute";
el.style.top = Math.floor(Math.random()*document.body.clientHeight);
el.style.left = Math.floor(Math.random()*document.body.clientWidth);
}
heyhey(document.getElementById('random'));
//random is the id of my html div
This works, but I want the function to be called every second
What I've tried to repeat the function:
function heyhey(el){
el.style.position = "absolute";
el.style.top = Math.floor(Math.random()*document.body.clientHeight);
el.style.left = Math.floor(Math.random()*document.body.clientWidth);
heyhey();
}
heyhey(document.getElementById('random'));
I also tried this:
function heyhey(el){
el.style.position = "absolute";
el.style.top = Math.floor(Math.random()*document.body.clientHeight);
el.style.left = Math.floor(Math.random()*document.body.clientWidth);
setTimeout(heyhey, 5000);
}
heyhey(document.getElementById('random'));
heyhey();