User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 422,662 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,683 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 1536 | Replies: 8 | Solved
Reply
Join Date: Jul 2005
Posts: 169
Reputation: aparnesh is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 10
aparnesh's Avatar
aparnesh aparnesh is offline Offline
Junior Poster

Question Cross-Browser setTimeout Problem

  #1  
Feb 12th, 2008
I have created a timer function using setTimeout. The function works perfectly in IE and Opera but doesn't work in FireFox or Netscape 6. I checked out my Javascript book where it says setTimeout is compatible with all browsers. So where am I going wrong ?

function showPics()
{
	if (i > 4)
		i = 0;
	var index = i;
	document.images("Img1").src = ImgBld[index];
	index = index + 1;
	if (index  > 4)
		index = index - 5;
	document.images("Img2").src = ImgBld[index];
	index = index + 1;
	if (index  > 4)
		index = index - 5;
	document.images("Img3").src = ImgBld[index];
	index = index + 1;
	if (index  > 4)
		index = index - 5;
	document.images("Img4").src = ImgBld[index];
	index = index + 1;
	if (index  > 4)
		index = index - 5;
	document.images("Img5").src = ImgBld[index];
	index = index + 1;
	if (index  > 4)
		index = index - 5;
	i = i + 1;
	setTimeout("showPics()",2000);
}

The function reads an array of image paths and displays the images in different Image tags. With the timer the images automatically change after every 2 secs. Works fine in IE & Opera, doesn't work at all in FF and NN.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2008
Location: Largo Florida
Posts: 269
Reputation: serkansendur is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 23
serkansendur's Avatar
serkansendur serkansendur is offline Offline
Posting Whiz in Training

Re: Cross-Browser setTimeout Problem

  #2  
Feb 12th, 2008
instead of accessing the images as collection items, use document.getElementById('imageID')
i dont know of opera but this works both in ie and firefox. Also since you are accessing the collection array, you might(i didnt try) use "[]" instead of "()". If you dont use document.getElementById method, try replacing the parathesis with brackets.
Serkan Şendur
MCAD.NET
Reply With Quote  
Join Date: Jul 2005
Posts: 169
Reputation: aparnesh is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 10
aparnesh's Avatar
aparnesh aparnesh is offline Offline
Junior Poster

Re: Cross-Browser setTimeout Problem

  #3  
Feb 13th, 2008
I don't think you understood the problem. The problem has nothing to do with arrays or getElementById or brackets. The problem is setTimeout function is working in IE and Opera and not FF or NN. Otherwise the code is fine
Last edited by aparnesh : Feb 13th, 2008 at 2:10 pm.
Reply With Quote  
Join Date: Jan 2008
Location: Largo Florida
Posts: 269
Reputation: serkansendur is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 23
serkansendur's Avatar
serkansendur serkansendur is offline Offline
Posting Whiz in Training

Re: Cross-Browser setTimeout Problem

  #4  
Feb 13th, 2008
Originally Posted by aparnesh View Post
I don't think you understood the problem. The problem has nothing to do with arrays or getElementById or brackets. The problem is setTimeout function is working in IE and Opera and not FF or NN. Otherwise the code is fine


I used setTimeout and it works both in ie and ff, www.altivi.com/shop go to the bottom of the page check if the lines get highlighted line by line. If it displays correctly, then this will be a proof that setTimeout works both in ie and ff, but i use latest version of firefox, what is your version?
Serkan Şendur
MCAD.NET
Reply With Quote  
Join Date: Jan 2008
Location: Bangalore, India
Posts: 336
Reputation: DangerDev is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 32
DangerDev's Avatar
DangerDev DangerDev is offline Offline
Posting Whiz

Help Re: Cross-Browser setTimeout Problem

  #5  
Feb 14th, 2008
hi
try this simple code it is working fine in both IE & FF:
  1. <html>
  2. <head>
  3. <title> simple count down</title>
  4.  
  5. <script language='javascript'>
  6. var i=0;
  7. var tid=0;
  8. function ut()
  9. {
  10. i++;
  11. document.frmT.txtB.value=i;
  12. tid=setTimeout("ut()",1000);
  13. }
  14. </script>
  15. </head>
  16. <body>
  17. <form name='frmT'>
  18. count down start now :
  19. <input type=text name='txtB'/>
  20. </form>
  21. <script>
  22. ut();
  23. </script>
  24. </body>
  25. </html>
A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila.
~Mitch Ratcliffe
Reply With Quote  
Join Date: Jan 2008
Location: Bangalore, India
Posts: 336
Reputation: DangerDev is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 32
DangerDev's Avatar
DangerDev DangerDev is offline Offline
Posting Whiz

Re: Cross-Browser setTimeout Problem

  #6  
Feb 14th, 2008
there might be problem in other lines u can check that by putting alert boxes between codes. if all are working then its fine.

u'r using () that could be problem use[], or better use getElementById()
coz setTimeOut() is compatible u can check my previous post.
A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila.
~Mitch Ratcliffe
Reply With Quote  
Join Date: Jul 2005
Posts: 169
Reputation: aparnesh is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 10
aparnesh's Avatar
aparnesh aparnesh is offline Offline
Junior Poster

Re: Cross-Browser setTimeout Problem

  #7  
Feb 14th, 2008
Thanks serkansendur and DangerDev. The problem was with the 'document.images(...)' part. I changed it to document.images[...] and it's working fine.
The display of '(' and '[' is almost similar in my Notepad font ('Bookman Old Style') so it was difficult to notice.
serkansendur, I apologise for my rather curt last message. You were absolutely right in pointing out the problem. My apologies
Reply With Quote  
Join Date: Jan 2008
Location: Largo Florida
Posts: 269
Reputation: serkansendur is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 23
serkansendur's Avatar
serkansendur serkansendur is offline Offline
Posting Whiz in Training

Re: Cross-Browser setTimeout Problem

  #8  
Feb 14th, 2008
Originally Posted by aparnesh View Post
Thanks serkansendur and DangerDev. The problem was with the 'document.images(...)' part. I changed it to document.images[...] and it's working fine.
The display of '(' and '[' is almost similar in my Notepad font ('Bookman Old Style') so it was difficult to notice.
serkansendur, I apologise for my rather curt last message. You were absolutely right in pointing out the problem. My apologies


Add to my reputation so i will forgive you then No problem, sometimes coding javascript makes me angry too. If you use visual studio .net,you can debug javascript although not as developed as debugging c#. Select tools >internet options > advanced in your ie browser window. In the advanced tab find "disable script debugging (internet explorer)", deselect the checkbox. Apply the changes then rerun your application in the debug mode. When javascript error occurs, internet explorer will ask you whether to debug the error. if you click yes then it will open up the visual studio editor and highlight the javascript line with the error notifying a simple error message like "null object" or "syntax error". Although not enough developed, this utility of visual studio makes javascript development easier.
Serkan Şendur
MCAD.NET
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,851
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 344
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Cross-Browser setTimeout Problem

  #9  
Feb 16th, 2008
> instead of accessing the images as collection items, use document.getElementById('imageID')

Actually it's the other way round. Instead of traversing the DOM tree it's better to use the images collection which holds a reference to all the images object in the document.
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 4:19 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC