Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

jQuery, Javascript, stop the execution of a animation / function

Posted on 2013-01-20
9
Medium Priority
?
401 Views
Last Modified: 2013-01-22
Hi E's. I have a script where will run a animation. The animation start automatic after load the page. The code have a "div stop" that if I click, the function demo() will stop execution (In my example the animation is approximately 10 seconds long (but can be much more time, and that is the reason for can stop the animation in the middle of the execution).
Here is my code:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<style type="text/css">
#geral{position: absolute; width: 600px; height: 600px;}
#piece{position: absolute; width: 13px; height: 20px; left: 20px; top: 150px;}

/* the new element (for show after the first stage):*/
#square{position: absolute; width: 20px; height: 20px; background-color: red; display:none; left: 500px; top: 500px;}
</style>
</head> 
<body> 
<div id="geral">
<div id="piece"><img src="ponteiro_rato.png" alt="Rato" width="13" height="20" /></div>
<div id="square"></div>
<div id="stop">stop demonstration</div>
</div>
<script>
demo();
function demo(){
$('#piece').animate({ left: '500', top: '500' }, 2500, function(){
      $('#square').show();
      var t = setTimeout(function(){
            $('#square').hide();
            $('#piece').animate({ left: '150', top: '100' }, 2500);
      },5000);
});
}

$("#stop").click(function(){
    //DO SOMETHING HERE TO STOP THE EXECUTION OF THE FUNCTION demo()
});
</script> 
</body> 
</html>
</body> 
</html>

Open in new window

What I have to write in line 34 for stop the execution of function demo()?

The best regards, JC
0
Comment
Question by:Pedro Chagas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 38799690
Use : http://api.jquery.com/stop/

So :
    $('#piece').stop();
or :
    $('#piece').stop(true, true);

Test page : http://jsfiddle.net/HyPFd/
0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 38800656
Hi, thanks for the answer. Just a clarification, in every situations will always use "stop"?
My doubt is about if have lots of "divs" in animation in simultaneous, I have to do "stop" for all. Maybe have another solution for stop all.
Can you give me a clarification?

~JC
0
 
LVL 82

Expert Comment

by:leakim971
ID: 38800759
use the same class for all your div your want to stop so instead using an ID :

$("#ID_OF_THE_DIV").stop();

use :

$(".CLASS_OF_THE_DIVS").stop();

bad design but work too :

$("#ID_OF_THE_DIV1,#ID_OF_THE_DIV3,#ID_OF_THE_DIV2").stop();
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 3

Author Comment

by:Pedro Chagas
ID: 38806946
Hi @leakim971 I did some change to my code, based in your last post:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<style type="text/css">
#geral{position: absolute; width: 600px; height: 600px;}
#piece{position: absolute; width: 13px; height: 20px; left: 20px; top: 150px;}

/* the new element (for show after the first stage):*/
#square{position: absolute; width: 20px; height: 20px; background-color: red; display:none; left: 500px; top: 500px;}
</style>
</head> 
<body> 
<div id="geral">
<div id="piece"><a class="anim"><img src="ponteiro_rato.png" alt="Rato" width="13" height="20" /></a></div>
<div id="square"><a class="anim"></a></div>
<div id="stop">stop demonstration</div>
</div>
<script>
demo();
function demo(){
$('#piece').animate({ left: '500', top: '500' }, 2500, function(){
      $('#square').show();
      var t = setTimeout(function(){
            $('#square').hide();
            $('#piece').animate({ left: '150', top: '100' }, 2500);
      },5000);
});
}

$("#stop").click(function(){
    $("#piece").stop();
});
</script> 
</body> 
</html>
</body> 
</html>

Open in new window

I associate the class "anim" to the divs #square and #peace.
The code in snippet code works well, but when I change the line 34 for "$(".anim").stop();" don't work. It was supposed to stop all elements with the class "anim"?

~JC
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 38806956
forget the class one, use : $("#ID_OF_THE_DIV1,#ID_OF_THE_DIV3,#ID_OF_THE_DIV2")
0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 38807007
Ok, I forget the class one.
You give me this solution, not yet tested ($("#ID_OF_THE_DIV1,#ID_OF_THE_DIV3,#ID_OF_THE_DIV2"), and I wonder:
Is possible do something like in that line:
$("#div*"); where the * is a div1 to div9?

~JC
0
 
LVL 82

Expert Comment

by:leakim971
ID: 38807026
wait, you have simpler, just use :
$("#stop").click(function(){
    $.fx.off = true;
});

Open in new window

0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 38807088
Don't work, but sounds good.
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<style type="text/css">
#geral{position: absolute; width: 600px; height: 600px;}
#piece{position: absolute; width: 13px; height: 20px; left: 20px; top: 150px;}

/* the new element (for show after the first stage):*/
#square{position: absolute; width: 20px; height: 20px; background-color: red; display:none; left: 500px; top: 500px;}
</style>
</head> 
<body> 
<div id="geral">
<div id="piece"><a class="anim"><img src="ponteiro_rato.png" alt="Rato" width="13" height="20" /></a></div>
<div id="square"><a class="anim"></a></div>
<div id="stop">stop demonstration</div>
</div>
<script>
demo();
function demo(){
$('#piece').animate({ left: '500', top: '500' }, 2500, function(){
      $('#square').show();
      var t = setTimeout(function(){
            $('#square').hide();
            $('#piece').animate({ left: '150', top: '100' }, 2500);
      },5000);
});
}


$("#stop").click(function(){
    $.fx.off = true;
});

</script> 
</body> 
</html>
</body> 
</html>

Open in new window


~JC
0
 
LVL 82

Expert Comment

by:leakim971
ID: 38807181
oh yes, it's to disable or not animation, this is not what you want
so use the previous one : $("#ID_OF_THE_DIV1,#ID_OF_THE_DIV3,#ID_OF_THE_DIV2")
if you want to follow your id $("#div*"); here the good syntax : $("[id^=div]").stop();
0

Featured Post

TCP/IP Network Protocol Cheat Sheet

TCP/IP is a set of network protocols which is best known for connecting the machines that make up the Internet. The truth is that TCP/IP is one of the oldest network protocols and its survival is mainly based on its simplicity and universality.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question