Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

setTimeout() not working?

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to do a very simple thing... I have an IFrame that I want to refresh every x number of seconds. To do that I have the following on a .jsp page that is included in another .jsp page:



What's happening is that the iframe is only ever loaded once. I've tried using:



and explicitly setting the src of my iframe, but that results in the src action being called twice - once when the iframe loads and once when the setTimeout() is called for the first (and last) time. Am I misunderstanding somethign about how setTimeout() is supposed to work? I'm testing this on IE 6 on Windows XP. Thanks!

--BW
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are after setInterval and not setTimeout....

Eric
 
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to use refresh meta tag instead of setInterval
 
Brian R. Wainwright
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys - the setInterval was what I was looking for since I just want to refresh a particular iframe. So this is working... sort of... My I fram relaods and calls my refresh-alerts servlet which is great. The servlet sets a session variable called "new-alerts" which I then want to use to update the html in a <span>. Here's the code... It doesn't work because the variables aren't refreshed whan I reload my iframe.



And elsewhere in my page...


I know I can't reference a session variable from my javascript and my scriptlet code gets compiled at runtime and never recompiles because it never refreshes (the iframe is all that refreshes). My question is how can I get the "val" variable to dynamically display the value getting set in my servlet? Hope this makes sense... I'm sure there's something simple I'm missing.
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is <%= newAlerts != null ? newAlerts.intValue() : 0 %> serverside code, only gets fired when the page loads from the server. I guessing you know that. If the value is in the iframe, you can use javascript to set the value such as

parent.functionName("theNewInfo");

Eric
 
Brian R. Wainwright
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Eric. I'm not sure I follow you when You say "if the value is in the iframe." Do you mean if it is within the <iframe></iframe> tags?

One idea I tried that actually works, but which looks like garbage is to have another iframe that has as its source a jsp which just has the following on it:



The iframe looks like:



Then in my reloadNotifications() function I do the following:


This works - the numbers update - because the server side scriptlet code get's reloaded when the ifram reloads. The only problem is look and feel - blech.
 
Brian R. Wainwright
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok guys - here's the final solution... In JSP #1 I have the following:



JSP #2 is the result of the call to "/drm/actions/refresh-alerts" as a Struts action. Essentially the content of the notificationFram ifram is this jsp...



So my reloadNotifications() call reloads the content of "frameData" and then I set the html contnet of the "myStuff" span to the html content of my iframe. Works like a charm!

Thanks everyone for their attention and help!
 
Yuriy Fuksenko
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try it...
Here is a problem:
parent.notificationFrame.window.location.reload();
This operation is asynchronious for the main documents, so you are likely sometimes get an error here:
str = window.frames['notificationFrame'].document.getElementById('frameData').innerHTML;
, if element "frameData" is not loaded yet.
I would say that chnages you need should be in the document loaded into iframe:
1. add the following inside the head tag - this will cause a page to refresh every 10 seconds:
<meta http-equiv="refresh" content="10">

2. add onload handler to the body, doing something like:
parent.document.getElementById("myStuff").innerHTML=document.getElementById('frameData').innerHTML;
[ August 08, 2006: Message edited by: Yuriy Fuksenko ]
 
this is supposed to be a surprise, but it smells like a tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic