I am an extreme newbie so apologies for an admittedly dumb question. But I have researched and tried several ways to make this function work and can't seem to get it. I am taking an online class and I am trying to build a simple app in which an image moves around the screen and the user tries to click it - and when they do click the image, the image disappears. I was able to construct the setInterval function but cannot get the clearInterval function to work.
(Apologies for admittedly political nature of code! Just a bit of US political humor. No offense intended!)
<h2>See if you can click on Hillary before she changes her position!</h2>
<p class="bold">This is your time:<span id="time">0</span>s</p>
<div id="box">
<img src="images/hillary.jpg" style="width:100%; height:100%">
</div>
<script type="text/javascript">
var time; var top; var left;
var makeBox;
function makeBox() {
time=Math.random();
time=1000*time;
setInterval(function () {
top=Math.random();
top=top*300;
left=Math.random();
left=left*1000;
document.getElementById("box").style.top=top+"px";
document.getElementById("box").style.left=left+"px";
document.getElementById("box").style.display="block";
createdTime=Date.now();
},time);
}
makeBox();
function myStopFunction() {
clearInterval(makeBox);
}
document.getElementById("box").onclick= function(){
clearInterval(makeBox);
this.style.display="none";
};
</script>
Justin