Put your code inside setInterval :
$(function () { setInterval(function () { $('#abovelogo').fadeIn(1000).delay(2000).fadeOut(1500).delay(2000).fadeIn(1500); }, 5000); });
Since you will run this as long as the page is active, you should do everything possible to optimize your code, for example, you can cache the selection outside the interval:
$(function () { var $element = $('#abovelogo'); setInterval(function () { $element.fadeIn(1000).delay(2000).fadeOut(1500).delay(2000).fadeIn(1500); }, 5000); });
Docs for setInterval : https://developer.mozilla.org/en/window.setInterval
Alternatively, instead of using .delay() you can use the callback functions in each animation to call one animation after another:
$(function () { var $element = $('#abovelogo'); setInterval(function () { $element.fadeIn(1000, function () { $element.fadeOut(1500, function () { $element.fadeIn(1500) }); }); }, 5000); });
Here is a demo: http://jsfiddle.net/xsATz/
You can also use setTimeout and call the function recursively:
$(function () { var $element = $('#abovelogo'); function fadeInOut () { $element.fadeIn(1000, function () { $element.fadeOut(1500, function () { $element.fadeIn(1500, function () { setTimeout(fadeInOut, 500); }); }); }); } fadeInOut(); });
Here is a demo: http://jsfiddle.net/xsATz/1/
source share