I'm creating this website with few mascots and I'll implement a "eye blink timer" where I'll make every mascot blink.
My question here is: how do i implement (and how long it is?) the delay between blinks and the blink itself, on any language (i'll probably use Javascript, but it doesn't matter right now).
Is there any resource about a "blink algorithm" or something like that?
Edit: I know how to use setTimeout and setInterval, my question here is more about the algorithm than the implementation itself.
Final result:
var blink = {
delay: function() {
return Math.random() * 8000 + 2000;
},
duration: function() {
return 100 + Math.floor(Math.random() * 100);
},
blinkAgain: function() {
return (Math.random() < .2);
},
betweenBliks: function() {
return blink.duration() / 2;
}
};
$.fn.blink = function(continueBlinking) {
var $element = $(this);
// Star the blink
$element.addClass('blink');
// Finish the blink
setTimeout(function() {
$element.removeClass('blink');
// Change of blinking again
if (blink.blinkAgain()) {
setTimeout(function() {
$element.blink(false);
}, blink.betweenBliks());
}
}, blink.duration());
// Continue blinking?
if (continueBlinking) {
setTimeout(function() {
$element.blink(true);
}, blink.delay());
}
};