I have one container div
which contains some children div
members. From time to time, I need to run a custom blinking effects on some of the child div
members. The effect steps are as follows:
- Change font color to Yellow
- Pause for 0.5 secs
- Change background color to yellow and text color to black
- Pause for 0.25 secs
- Change font color to Yellow & background to black
- Pause for 0.25 secs
- Change background color to yellow and text color to black
- Pause for 0.5 secs
- Repeat step 1 to 8
Below is the JavaScript code that I came up using jQuery to achieve the above effect. The code is working perfectly fine, but somehow I feel that this is not an elegant solution and there may be possible issue with how I pass $elements
var
from one function to another.
Is there anything wrong with this approach? How can this code be improved?
function startAnimation($container) {
var $elements = $container.find(".animation");
if ($elements.length) {
$elements.switchClass("mc-ylw-blk", "mc-blk-ylw", 0).show(0).delay(500).promise().done(function () {
$elements.switchClass("mc-blk-ylw", "mc-ylw-blk", 0).delay(250).promise().done(function () {
$elements.switchClass("mc-ylw-blk", "mc-blk-ylw", 0).delay(250).promise().done(function () {
$elements.switchClass("mc-blk-ylw", "mc-ylw-blk", 0).delay(500).promise().done(function () {
startAnimation($container); //Repeat
});
});
});
});
}
}
-
1\$\begingroup\$ Hi, and welcome to Code Review. Your question would benefit from having a JSFiddle to illustrate your task. Have you considered putting one together? \$\endgroup\$rolfl– rolfl2014年05月22日 20:38:44 +00:00Commented May 22, 2014 at 20:38
-
\$\begingroup\$ I already got the answer now. Next time I will definitely consider the jsfiddle. \$\endgroup\$indusBull– indusBull2014年05月27日 21:16:13 +00:00Commented May 27, 2014 at 21:16
3 Answers 3
you can chain the delay and switchClass to $elements and take the $("#container").find(".animation"); outside the function like this
function startAnimation($elements) {
$elements.switchClass("mc-ylw-blk", "mc-blk-ylw", 0)
.delay(500).switchClass("mc-blk-ylw", "mc-ylw-blk", 0)
.delay(250).switchClass("mc-ylw-blk", "mc-blk-ylw", 0)
.delay(250).switchClass("mc-blk-ylw", "mc-ylw-blk", 0)
.delay(500).promise().done(function () {
startAnimation($elements);
});
}
var $elements=$("#container").find(".animation");
if ($elements.length) {
startAnimation($elements);
}
-
\$\begingroup\$ Oh man.. thank you!! Now I'm wondering why I didn't think about this at first place. \$\endgroup\$indusBull– indusBull2014年05月27日 21:15:18 +00:00Commented May 27, 2014 at 21:15
Here is a version using setInterval() set at 250 at the second iteration and the sixth(in a set of 6) it skips thus taking twice the time, and then uses the modulus operator(dividing by 2 this time) to detirmine the proper class to display.
var $elements = $("#container").find(".animation");
var i = 0, x = 0;
if ($elements.length) {
setInterval(function(){
x++;
if((x % 6) == 1 || (x % 6) == 5) return;
class1 = ((i % 2) == 0) ? "mc-ylw-blk" : "mc-blk-ylw";
class2 = ((i % 2) == 1) ? "mc-ylw-blk" : "mc-blk-ylw";
$elements.switchClass(class1, class2, 0);
i++;
}, 250);
}
And here is the jsFiddle: http://jsfiddle.net/rWBa7/
-
1\$\begingroup\$ I fee that this code is more complicated and unreadable. Makes it harder to change it in future if requirement changes. \$\endgroup\$indusBull– indusBull2014年05月27日 21:13:19 +00:00Commented May 27, 2014 at 21:13
It would probably be more efficient to simply put all the logic into a loop rather than calling it recursively. You end up with a million copies of that function running until the page closes otherwise.