0

After the window is loaded, a function is called using the event onload="".

I have another event, onclick="", which should pause or resume the previous function whenever I click on the window.

I read this thread and implemented the logic for pause/resume functionality but for some reason the previous function was not paused/resumed.

My html and javascript code looks like this:

<!DOCTYPE html>
<html lang="en">
 <head>
 <script type="text/javascript">
 var started = false;
 var timer;
 function initialize(){
 do something...
 timer = setInterval(move,15);
 function move(){ //it animates, it moves div elements on the window
 do something...
 }
 }
 function pauseResume(){ //it should pause the elements and resume 
 //from the same coordinate(x,y)
 //shouldn't resume the animation from the begining
 if(started){
 clearTimeout(timer);
 started = false;
 }else{
 initialize();
 started = true;
 }
 }
 </script>
 </head>
 <body onload="initialize();">
 <div onclick="pauseResume(event);"></div>
 </body>
 </html>

Can't use jQuery or any other javaScript libraries. Is there something wrong with my implementation?

asked Sep 21, 2019 at 19:35
1
  • 2
    I haven't looked in detail, but this is wrong: var timer = setInterval(move,15); - you need to remove the var, so that it references the timer in the outer scope. The way you have it now, the clearInterval will not clear the timer. Commented Sep 21, 2019 at 19:45

1 Answer 1

1

i agree with a Robin comment, I would rewrite the code to this way:

var started = false;
var timer;
function initialize(){
 do something...
 timer = setInterval(move,15);
}
function move(){ //it animates, it moves div elements on the window
 do something...
}
function pauseResume(){ //it should pause the elements and resume 
 //from the same coordinate(x,y)
 //shouldn't resume the animation from the begining
 if(started){
 clearTimeout(timer);
 started = false;
 }else{
 initialize();
 started = true;
 }
}
answered Sep 21, 2019 at 19:56
Sign up to request clarification or add additional context in comments.

3 Comments

that was a typo, I edited the code. The problem is when I click on the window the animation(movement of the div elements) pauses but when I click again to resume, the animation starts from the beginning. Do you have any clue how to resume the animation from the exact point where it was paused?
@007mrviper you need to save the current state of your animation process. Whatever information you have which would be needed to continue the next step (e.g. coordinates or anything else) needs to be stored at the point of pausing in such a way that you can retrieve it next time you resume. This is basically what all games and programs of that type do when you save a game. In your case you could put the data in a simple global object, or in localstorage or something like that.
I got you, you need to see this article there are a lot of examples of how to build such animations

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.