Cross-Browser setTimeout Problem

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Jul 2005
Posts: 188
Reputation: aparnesh is an unknown quantity at this point 
Solved Threads: 10
aparnesh's Avatar
aparnesh aparnesh is offline Offline
Junior Poster

Cross-Browser setTimeout Problem

 
0
  #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 ?

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function showPics()
  2. {
  3. if (i > 4)
  4. i = 0;
  5. var index = i;
  6. document.images("Img1").src = ImgBld[index];
  7. index = index + 1;
  8. if (index > 4)
  9. index = index - 5;
  10. document.images("Img2").src = ImgBld[index];
  11. index = index + 1;
  12. if (index > 4)
  13. index = index - 5;
  14. document.images("Img3").src = ImgBld[index];
  15. index = index + 1;
  16. if (index > 4)
  17. index = index - 5;
  18. document.images("Img4").src = ImgBld[index];
  19. index = index + 1;
  20. if (index > 4)
  21. index = index - 5;
  22. document.images("Img5").src = ImgBld[index];
  23. index = index + 1;
  24. if (index > 4)
  25. index = index - 5;
  26. i = i + 1;
  27. setTimeout("showPics()",2000);
  28. }

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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 123
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: Cross-Browser setTimeout Problem

 
0
  #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.
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 188
Reputation: aparnesh is an unknown quantity at this point 
Solved Threads: 10
aparnesh's Avatar
aparnesh aparnesh is offline Offline
Junior Poster

Re: Cross-Browser setTimeout Problem

 
0
  #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 3:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 123
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: Cross-Browser setTimeout Problem

 
0
  #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?
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 483
Reputation: DangerDev has a spectacular aura about DangerDev has a spectacular aura about 
Solved Threads: 59
DangerDev's Avatar
DangerDev DangerDev is offline Offline
Posting Pro in Training

Re: Cross-Browser setTimeout Problem

 
0
  #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>
Freedom in the Mind, Faith in the words.. Pride in our Souls...
Indian Developer
http://falaque.wordpress.com/
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 483
Reputation: DangerDev has a spectacular aura about DangerDev has a spectacular aura about 
Solved Threads: 59
DangerDev's Avatar
DangerDev DangerDev is offline Offline
Posting Pro in Training

Re: Cross-Browser setTimeout Problem

 
0
  #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.
Freedom in the Mind, Faith in the words.. Pride in our Souls...
Indian Developer
http://falaque.wordpress.com/
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 188
Reputation: aparnesh is an unknown quantity at this point 
Solved Threads: 10
aparnesh's Avatar
aparnesh aparnesh is offline Offline
Junior Poster

Re: Cross-Browser setTimeout Problem

 
0
  #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 Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 123
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: Cross-Browser setTimeout Problem

 
0
  #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.
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,653
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Cross-Browser setTimeout Problem

 
0
  #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.
The romantic image of an über-programmer is someone who fires up Emacs, types like a machine gun, and delivers a flawless final product from scratch. A more accurate image would be someone who stares quietly into space for a few minutes and then says “Hmm. I think I’ve seen something like this before.” - John D
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 2
Reputation: Nine of Hearts is an unknown quantity at this point 
Solved Threads: 0
Nine of Hearts Nine of Hearts is offline Offline
Newbie Poster

Re: Cross-Browser setTimeout Problem

 
0
  #10
Jul 22nd, 2009
I'm currently tearing my hair out with this one. On my website the icon links to other pages "spin" when hovered over. They actually just squish sideways, but it looks like they spin. Anyway, this works well in IE and sort of in Firefox. Except for the index.php page, on there the exact (it's called via an include) same code doesn't work. Why oh why oh why?

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function xspin()
  2. {
  3. var t_time = 0;
  4. var i_time = 10;
  5. for (i_phse = 0; i_phse <= i_time; i_phse++)
  6. {
  7. i_time += 60;
  8. i_wdth = Math.abs(Math.cos((i_phse/(i_time/2)) * Math.PI) * 48);
  9. t_actn = setTimeout("document.ikon01.style.width = " + i_wdth + "; document.ikon01.style.height = 48;", t_time);
  10. }
  11. }

Like I say, the above works on certain pages, but not the index page. On the test version the index page is now virtually just

<head>
</head>
<body>
< include....
</body>

so I don't think it's clashing with other code on the page, beacuse there isn't any! Any help greatly appreciated.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



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


Views: 4200 | Replies: 11
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC