- Simple Hide and Show with Prototype
- Event Observe and Event Listeners with Prototype
- JavaScript Script.aculo.us Fade with a Timer
- Evolving Styles: Add, Change Remove Classes with Prototype
- Alternating Colors Using Prototype
- First, Last, Active and Only List Styles With Prototype
- Change Rel External to Target Blank using Prototype
- Zip Code, Phone Number and Replacing Empty Fields with Really Easy Validation
- Limit Characters in a Textarea with Prototype
- Open Links to a Different Site in a New Window with Prototype
Learned a new JavaScript thing today: using a timer with the Script.aculo.us library!
It’s nothing terribly fancy but it took me a little to track down everything and in the end it was so easy.
I used a simple JavaScript timer to trigger a function that faded text out.
HTML & JavaScript
<script type="text/javascript">
Event.observe(window, 'load', function() {
var fade=setTimeout("fadeout()",3500);
var hide=setTimeout("$('message').hide()",4800);
});
function fadeout(){
new Effect.Opacity("message", {duration:1.5, from:1.0, to:0.0});
}
</script>
<div id="message">Your email has been sent</div>
I’ll walk you through the process I used to create this snippet.
Create the Fade Out Function
I used the Script.aculo.us opacity effect in a simple JavaScript function that went from 100% opaque to 0% opaque in 1.5 seconds.
JavaScript
function fadeout(){
new Effect.Opacity("message", {duration:1.5, from:1.0, to:0.0});
}
Start JavaScript Timer on Page Load
Next I used the Prototype Observe Event to get the timer started once the page has finished loading and then hide the div using Prototype’s hide function just before the text fades all the way out.
JavaScript
Event.observe(window, 'load', function() {
var fade=setTimeout("fadeout()",3500);
var hide=setTimeout("$('emailmessage').hide()",4800);
});
- Page loads at 0 milliseconds
- Text begins to fade out at 3500 milliseconds (3.5 seconds after the page finishes loading)
- Text is hidden at 4800 milliseconds (4.8 seconds after the page finishes loading)
- Text finishes it’s fade at 5000 milliseconds (5 seconds after the page finishes loading)
Pretty easy and very effective for feedback that a person only needs for just a moment and requires no additional action.
Read more about JavaScript timers at W3C Schools.
Prototype 1.6 Delay() Update
Updated 2007 Dec 19
Yesterday I noticed that Prototype made some major updates to the library in November so I thought I’d update the info in this tutorial to incorporate the new Prototype delay() function.
You can replace the HTML / JavaScript combination above with the following.
HTML & JavaScript
<script type="text/javascript">
Event.observe(window, 'load', function() {
fadeout.delay(3.5);
Element.hide.delay(4.8, "message");
});
function fadeout(){
new Effect.Opacity("message", {duration:1.5, from:1.0, to:0.0});
}
</script>
<div id="message">Your email has been sent</div>
Doesn’t really reduce the space or runtime as far as I know but it’s an update. It’s nice to stay current with new features because you never know when you’ll find a use for them that far surpasses the old way of thinking.
Summary
Prototype Functions Used
- Element.delay() new in v.1.6
- Element.hide()
{ 2 comments… read them below or add one }
I am working on the design of the above website. so you will not see the issue that I’m having with the website.
On my new website I am using a video which is a large file so every time I go to the website it takes long time to load the page and background image. I used your javascript timer on page load code on the video clip. it’s working now. It doesn’t take long time that it used to but there is a runtime error saying “event is undefined”.
I am very new in javascript. Would you please help me to fix the problem. Thank you
I know this is an older blog and I stumbled upon it from another site – looking for a fade code to use with a timer. My question is this: How do you account for the different browsers out there that may or may not opperate the same? IE has a different setup than FF or Safari. So how do you incorporate a browser detect function with your timer?
-mb
{ 1 trackback }