0

Not certain what options I have, I want to run a loop within a controller that runs in the background and steps every n'th milli seconds, it will just change the colour on a button, want it to go from green to orange and then back using ng-style.

What is a sensible approaches to running a loop in the background, for obvious reasons I would like to avoid locking up the whole page just to have a button changing colour.

1
  • I would really want to thank for everyones input. In this particular case, I wanted to use angular if possible, as there is quite a few things that needs to happen on the angular front and the animations will eventually be dependent on code in angular. Besides I want to get better at that framework. Certain all the other solutions works as well. Commented Apr 19, 2015 at 4:36

4 Answers 4

4

Since you're using Angular, you should use the Angular solution: $interval.

A simple example, assuming you're using a boolean in the scope to determine whether the button is green or orange:

angular.module('myApp', [])
    .controller('myController', ['$scope', '$interval',
        function($scope, $interval) {
            $scope.isGreen = true;

            $interval(function() {
                $scope.isGreen = !$scope.isGreen;
            }, 1000);
        }
    ]);

This will toggle $scope.isGreen every second.

Sign up to request clarification or add additional context in comments.

1 Comment

Damn right! If using AngularJS $interval is the way to go
2

I think perhaps a css option might be the way to go here. You can loop animation in css.

Keyframe Animation Syntax

Here's an example

@-webkit-keyframes looping-background {
  0%   { background-color: orange; }
  50% { background-color: green; }
  100% { background-color: orange; }
}
@-moz-keyframes looping {
  0%   { background-color: orange; }
  50% { background-color: green; }
  100% { background-color: orange; }
}
@-o-keyframes looping {
  0%   { background-color: orange; }
  50% { background-color: green; }
  100% { background-color: orange; }
}
@keyframes loopingN {
  0%   { background-color: orange; }
  50% { background-color: green; }
  100% { background-color: orange; }
}

.loop_animation {
  -webkit-animation: looping-background 2s infinite; /* Safari 4+ */
  -moz-animation:    looping-background 2s infinite; /* Fx 5+ */
  -o-animation:      looping-background 2s infinite; /* Opera 12+ */
  animation:         looping-background 2s infinite; /* IE 10+, Fx 29+ */
}
<button class="loop_animation">I am a button</button>

Comments

1

Use a javascript event timer to call a function.

https://developer.mozilla.org/en-US/Add-ons/Code_snippets/Timers

Comments

1

why dont u you use

setTimeout(function(){
                          // change the color of button here
                          }),n*1000);

this set time out should be called in recursive loop .

<body onload = "changeButtonColor()">

<script>
function(){
         // change the color here 
         setTimeout(function(){
                          // change the color of button here
                          }),n*1000);
         }
</script>

Comments

Your Answer

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.