I want to stop my setInterval
by clicking on h1 element which contain stop
in its content, but It seems I can't make it happen.
I can see value of variable of setInterval which is return in callOff
variable on console inside and outside click function, but my h1 element which has heading
id keeps moving.
Even I decaler callOff
globally, yet nothing has change.
Could anyone tell me what I am doing wrong?
var callOff;
var move = function() {
$("html").mousemove(function(event) {
$("#heading").offset({
left: event.pageX,
top: event.pageY
});
});
};
var callOff = setInterval(move, 1000);
console.log(callOff);
$("#stop").click(function() {
console.log("I am in click " + callOff);
clearInterval(callOff);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<h1 id="heading">Hello world!</h1>
<h1 id="stop">Stop !</h1>
Note:
May be my approach is not one of the best one, but right now I am learning. I am asking a question and I want help for what I am looking for and not what is the best solution. I really do not get why some people vote down a good quality question either.