| | |
swap_image(): failed
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
Hello everyone. I'm trying to write some Javascript code to make a logo on my website flash periodically between two differently coloured versions (i.e. a simple "swap image - restore image job). Here is a simplified version of what I am attempting, with no loops added yet:
The featured script fails to do anything. I have read some tutorials on w3schools and checked for obvious errors but can't find anything. If anyone can point out my mistake here I would really appriciate it. Thanks.
Steven.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<html> <head> <title>Rochfest Preview Portal (RPP)</title> <script language="javascript" type="text/javascript"> function swap_image() { if (flag1 == 1) { document.getElementById("img1").src="rochfest-logo2.gif"; } else if (flag1 == 2) { document.getElementById("img1").src="rochfest-logo.gif"; }; } </script> </head> <body> <p align="center"><img src="rochfest-logo.gif" id="img1"></p> <script language="javascript" type="text/javascript"> var a = 0; var flag1 = 1; a = setTimeout("swap_image()", 1000); flag1 = 2; a = setTimeout("swap_image()", 1000); </script> </body> </html>
The featured script fails to do anything. I have read some tutorials on w3schools and checked for obvious errors but can't find anything. If anyone can point out my mistake here I would really appriciate it. Thanks.
Steven.
•
•
Join Date: Sep 2006
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
Hello everyone. I'm trying to write some Javascript code to make a logo on my website flash periodically between two differently coloured versions (i.e. a simple "swap image - restore image job). Here is a simplified version of what I am attempting, with no loops added yet:
The featured script fails to do anything. I have read some tutorials on w3schools and checked for obvious errors but can't find anything. If anyone can point out my mistake here I would really appriciate it. Thanks.JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<html> <head> <title>Rochfest Preview Portal (RPP)</title> <script language="javascript" type="text/javascript"> function swap_image() { if (flag1 == 1) { document.getElementById("img1").src="rochfest-logo2.gif"; } else if (flag1 == 2) { document.getElementById("img1").src="rochfest-logo.gif"; }; } </script> </head> <body> <p align="center"><img src="rochfest-logo.gif" id="img1"></p> <script language="javascript" type="text/javascript"> var a = 0; var flag1 = 1; a = setTimeout("swap_image()", 1000); flag1 = 2; a = setTimeout("swap_image()", 1000); </script> </body> </html>
Steven.
However, if you are determined to use javascript then it can be done. Just be aware that with more complex effects than you are trying to achieve, setTimeout and image replacement can have strange side-effects and can cause many headaches.
The key to your problem is that the setTimeout function acts asynchronously - that is, your code will keep executing while the delay is occuring. So, in your case the entire code will be completed before the first image substitution is done, but there will only be a fraction of a second between the first and second image replacement, so you will only see the last one.
The solution is to create a function which does the image replacement and then calls setTimeout, which invokes a fuction (the same one with the name of the image file changed) which does image replacement and calls setTimeout, which...... you should get the idea. This will result in an animation which goes on until the user leaves the page. I have not tried to code it for this answer, but in rough pseudo-code:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
function animate { if imagefilename == file1 then imagefilename = file2 else imagefilename = file1 do image relacement setTimeout ("animate()", 1000) }
I think something like that will work - try it and see. Good luck.
Last edited by tgreer; Sep 19th, 2006 at 10:30 am. Reason: added code tags
![]() |
Similar Threads
- Floppy test failed! (Windows NT / 2000 / XP)
- s.m.a.r.t. command failed (Windows 95 / 98 / Me)
- How delete failed W2000 installation (Windows NT / 2000 / XP)
- Iexplore.exe application failed?? (Web Browsers)
- Win XP - Application Failed to Initialize (Windows NT / 2000 / XP)
- Tried to install Panther. Failed (OS X)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: maps.google.com
- Next Thread: Which scripting language should I prefer VBScript or Javascript?
| Thread Tools | Search this Thread |
ajax ajaxcode ajaxexample ajaxhelp ajaxjspservlets animate array automatically browser bug calendar captchaformproblem checkbox child class close codes cookies createrange() cursor date debugger dependent disablefirebug dom dropdown editor element embed engine events explorer ext file firefox form forms getselection google gxt hiddenvalue highlightedword hint html htmlform ie7 ie8 iframe images internet java javascript javascripthelp2020 jawascriptruntimeerror jquery jsf jsfile jsp jump libcurl maps masterpage math media microsoft object onmouseoutdivproblem onreadystatechange parent paypal pdf php player position post programming progressbar redirect regex runtime safari scriptlets scroll search security session shopping size software sql text textarea toggle unicode variables web website windowsxp wysiwyg \n





