473,394 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

javascript setInterval()

Having a problem with setTimeout("doSomething()", 5000) or, preferably, setInterval("doSomething()",5000), referring to a function:
doSomething() { // do something here }
-- where the do something is document.write("whatever") in order to write "whatever" to the screen repeatedly every 5 seconds. (Works okay for writing to a text box). Internet Explorer writes once, then gives message "Error on page"
May 15 '07 #1
8 4212
pbmods
5,821 Expert 4TB
Having a problem with setTimeout("doSomething()", 5000) or, preferably, setInterval("doSomething()",5000), referring to a function:
doSomething() { // do something here }
-- where the do something is document.write("whatever") in order to write "whatever" to the screen repeatedly every 5 seconds. (Works okay for writing to a text box). Internet Explorer writes once, then gives message "Error on page"
Here's what I'm thinking:

When you call document.write, it has to open the document object for writing. If you've already loaded a page, then it basically deletes the whole document and starts over. If you declared your code in the body of the script, then your script will be deleted (in the browser window, not your source code), and so when the browser goes to doSomething() 5 seconds later, doSomething doesn't exist.

Try either declaring your script in the document's head, or better yet, have doSomething create a node and append it to the body:

Expand|Select|Wrap|Line Numbers
  1. function doSomething() {
  2.     var $newText = document.createElement('div');
  3.     $newText.appendChild(document.createTextNode('Hi!'));
  4.     document.body.appendChild($newText);
  5. }
  6.  
May 15 '07 #2
Thankyou, pbmods. (Sorry to have been slow trying your suggestion). Your code to create a node works perfectly. ..Is there a way of displaying the "Hi!" in random fonts, colours and sizes, in random placements on the screen? I know how to do this with document.write(name) but can't get it to work with the text node?? (What I'm working towards is amusing a child by by displaying his name in this manner).
Re the other suggestion of declaring the script in the document's head, I thought I was doing that: where did I go wrong?
<HTML> <HEAD>.... <SCRIPT TYPE = "text/javascript">
function doSomething()
{
document.write("Hi!");
setTimeout("doSomething()",5000);
}
</SCRIPT>
</HEAD>
<BODY> etc
May 22 '07 #3
dmjpro
2,476 2GB
u can do this by changing the style property at 5 seconds interval.

have a good day.

kind regards.
dmjpro.
May 22 '07 #4
dmjpro
2,476 2GB
Expand|Select|Wrap|Line Numbers
  1. function doSomething() {
  2.     var $newText = document.createElement('div');
  3.     $newText.appendChild(document.createTextNode('Hi!'));
  4.     document.body.appendChild($newText);
  5. }
  6.  
this type declaration ($newText) .... does it mean something different than newText?????

plz help PBMODS.

kind regards.
dmjpro.
May 22 '07 #5
u can do this by changing the style property at 5 seconds interval.

have a good day.

kind regards.
dmjpro.
Thanks for your help
derrickn
May 26 '07 #6
pbmods
5,821 Expert 4TB
this type declaration ($newText) .... does it mean something different than newText?
Nope. No difference whatsoever (except that '$newText' != 'newText' of course). I'm just a hardcore PHP guy, so I like to put a '$' in front of my variables so that I can easily identify them.
May 26 '07 #7
Thanks for your help
derrickn
dmjpro Could I request further help? I was able to select a font color and a background color by inserting (into pbmods code for creating a node) lines such as: document.body.style.color="red"; // this changed the font color, though
ditto.fontColor="red'" had no effect.
documen.body.style.backgroundColor="yellow"; // worked for background color
Question: how to set a different font face and size, and a different placement on the screen? Nothing I tried with document.body.style. worked for these things.
Thanks, derrickn
May 27 '07 #8
pbmods
5,821 Expert 4TB
Question: how to set a different font face and size, and a different placement on the screen? Nothing I tried with document.body.style. worked for these things.
Font face is 'fontFamily', and font size is fontSize. Take a look at this reference here.

Note that when you have a hyphenated word (such as font-family), you have to camel-case it to use it in JavaScript (e.g., fontFamily).
May 27 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: tshad | last post by:
I posted this on the asp.net group, also. I wasn't sure whether this was an asp.net problem or a javascript problem. I have a page that was originally created from a program I found on the net...
10
by: Chris Lieb | last post by:
Hi, I am working on an object that has a function that calls setInterval so that it can have a function called repeatedly. Right now, to call the function, you also have to provide the name of...
4
by: E | last post by:
I am having trouble with setTimeout working on a second call to the setTimeout function from a second page which is an html page. Here is the scenario. I have a web page and onload it calls a...
10
by: jason_box | last post by:
Hello, I was wondering if there was a way to have a javacript be activated by an input button that would call to a cgi program and querey every 10minutes and the cgi would update the page without...
2
by: alxasa | last post by:
Hi, I have a setInterval which executes its command every 10 seconds in a infinite loop. I've got something real basic like: var processes=0; function startme(){ if(stopthisloop>1)
3
by: Beamer | last post by:
Hi I am trying to build a roating slide effect in javascript. Basically, I have a list like below <ul id="slideShowCnt"> <li id="slide0"><img .../></li> <li id="slide0"><img .../></li> <li...
3
by: Daz | last post by:
Hi everyone. I have a relatively simple question which is hindered only by my lack of understanding. I am creating a clock (as if that's never been done before...), and I was stuck as to...
10
by: Evan Charlton | last post by:
Hey all, I'm having some trouble with window.setInterval() within a custom object/prototype. Here is my code: function MyClass() { // do some junk // ... // define methods this.m_one =...
13
by: mudgen | last post by:
I have a webpage that has two flash animations running. I also have a javascript script running that rotates 3 images and fades them in and out. To do the fading and rotating in javascript I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.