Solved

Issue with timed animation

Posted on 2014-12-10
8
145 Views
Last Modified: 2014-12-17
Hi, I'm new to jQuery and I'm trying to create different timed animations that run independently from each other, I found a really well made animation code and I adapted it for my needs. I have created an jsFiddle example to show the creation of the animations inside a list item. The problem I'm having is that if I click on one green timed circle, the others stop. I don't know how to instantiate them for each row...

Here is the jsfiddle: JSFIDDLE

Some code:

var methods = {
     init: function (options) {
         var state = {
             timer: null,
             timerSeconds: 60,
             callback: function () {},
             timerCurrent: 0,
             showPercentage: false,
             fill: false,
             color: '#CCC'
         };

         state = $.extend(state, options);

         return this.each(function () {
             var $this = $(this);
             var data = $this.data('pietimer');
             if (!data) {
                 $this.addClass('pietimer');
                 $this.css({
                     fontSize: $this.width()
                 });
                 $this.data('pietimer', state);
                 if (state.showPercentage) {
                     $this.find('.percent').show();
                 }
                 if (state.fill) {
                     $this.addClass('fill');
                 }
                 $this.pietimer('start');
             }
         });
     },

     stopWatch: function () {

         var data = $(this).data('pietimer');
         if (data) {
             var seconds = (data.timerFinish - (new Date().getTime())) / 1000;
             if (seconds <= 0) {
                 clearInterval(data.timer);
                 $(this).pietimer('drawTimer', 100);
                 data.callback();
             } else {
                 var percent = 100 - ((seconds / (data.timerSeconds)) * 100);
                 $(this).pietimer('drawTimer', percent);
             }
         }
     },

     drawTimer: function (percent) {

         $this = $(this);
         var data = $this.data('pietimer');
         if (data) {
             $this.html('<div class="percent"></div><div class="slice' + (percent > 50 ? ' gt50"' : '"') + '><div class="pie"></div>' + (percent > 50 ? '<div class="pie fill"></div>' : '') + '</div>');
             var deg = 360 / 100 * percent;
             $this.find('.slice .pie').css({
                 '-moz-transform': 'rotate(' + deg + 'deg)',
                     '-webkit-transform': 'rotate(' + deg + 'deg)',
                     '-o-transform': 'rotate(' + deg + 'deg)',
                     'transform': 'rotate(' + deg + 'deg)'
             });
             var secs = (data.timerSeconds) * ((100 - percent) / 100); /*NEW*/
             $this.find('.percent').html(Math.round(secs) + ''); /*Changed*/
             if (data.showPercentage) {
                 $this.find('.percent').show();
             }
             if ($this.hasClass('fill')) {
                 $this.find('.slice .pie').css({
                     backgroundColor: data.color
                 });
             } else {
                 $this.find('.slice .pie').css({
                     borderColor: data.color
                 });
             }
         }
     },

     start: function () {

         var data = $(this).data('pietimer');
         if (data) {
             data.timerFinish = new Date().getTime() + (data.timerSeconds * 1000);
             $(this).pietimer('drawTimer', 0);
             data.timer = setInterval("$this.pietimer('stopWatch')", 50);
         }
     },

     reset: function () {
         console.log("flag 4");
         var data = $(this).data('pietimer');
         if (data) {
             clearInterval(data.timer);
             $(this).pietimer('drawTimer', 0);
         }
     }
 };

 $.fn.pietimer = function (method) {

     if (methods[method]) {
         return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
     } else if (typeof method === 'object' || !method) {
         return methods.init.apply(this, arguments);
     } else {
         $.error('Method ' + method + ' does not exist on jQuery.pietimer');
     }
 };



 function runTimer() {

     $('.timer').pietimer({
         timerSeconds: 60,
         color: '#ccc',
         fill: '#ccc',
         showPercentage: true,
         callback: function () {
             console.log("flag 7");
             // alert("yahoo, timer is done!");
             $('.timer').pietimer('reset');
             $this.find('.percent').html(0);
         }
     });
 }

Open in new window


Thanks in advance.
0
Comment
Question by:claire89
[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
8 Comments
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 40492366
looks nice :) but are you sure this is intended to run multiple instances?
Maybe it is written as one time use... and you are trying to make it run multiple simultaneously...

which will require tons of change (passing id's to all functions etc)
0
 

Author Comment

by:claire89
ID: 40492800
I would like it to run multiple instances but i lack the knowledge to do so :)
0
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 40496091
Please update the fiddle. It is not loading.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 43

Expert Comment

by:Rob
ID: 40497123
The fiddle wasn't loading due to the jsfiddle website having issues.

To instantiate you'll need to wrap this up in a class that you can separate with the "new" operator

http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_13138-Javascript-is-just-an-Object.html

Working on something for you now.
0
 

Author Comment

by:claire89
ID: 40497793
Thanks really apreciate the help
0
 
LVL 43

Accepted Solution

by:
Rob earned 500 total points
ID: 40498419
Ok, here is a link to a jsbin demo: http://jsbin.com/yikeze/1/edit?console,output

I've unbound it from jQuery to allow for multiple instances.  I've wrapped the pietimer into its own class (timer.js attached).

any questions let me know.
index.html
timer.js
main.js
main.css
0
 

Author Closing Comment

by:claire89
ID: 40504400
Many thanks :)
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…

738 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