473,320 Members | 1,961 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,320 software developers and data experts.

Javascript: Changing text inside a table with setInterval

12
I am trying to change the text continously for the text inside a table but it stops after reaching the 2nd index in the array. Anyone can debug? Below are the coding:


<script language="javascript">

var i=0;
var text=["Chocolate", "Vanilla", "Strawberry"];

function dhtml(){
document.write("<table border='1' color='black'>");
document.write("<tr align='center' bgcolor='grey'>");
document.write("<td colspan='300'>");
document.write("<font size='10' color='red'><b>");
// if(i >= 3) {
// i = 0;}
document.write(text[i]);
document.write("</b></font>");
document.write("</td>");
document.write("</table>"); i++;}

setInterval('dhtml()', 1000);
dhtml();
// clearInterval(id);


</script>
Jan 8 '07 #1
4 8483
b1randon
171 Expert 100+
I am trying to change the text continously for the text inside a table but it stops after reaching the 2nd index in the array. Anyone can debug? Below are the coding:


<script language="javascript">

var i=0;
var text=["Chocolate", "Vanilla", "Strawberry"];

function dhtml(){
document.write("<table border='1' color='black'>");
document.write("<tr align='center' bgcolor='grey'>");
document.write("<td colspan='300'>");
document.write("<font size='10' color='red'><b>");
// if(i >= 3) {
// i = 0;}
document.write(text[i]);
document.write("</b></font>");
document.write("</td>");
document.write("</table>"); i++;}

setInterval('dhtml()', 1000);
dhtml();
// clearInterval(id);


</script>
You're reloading the page every time so you reinitialize everything. You want to be doing something like this:
Expand|Select|Wrap|Line Numbers
  1. var i=0;
  2. var text=["Chocolate", "Vanilla", "Strawberry"];
  3.  
  4. function dhtml(){
  5.     document.getElementById('container').innerHTML = text[i];
  6.     i++;
  7.     if(i>2)
  8.         i=0;
  9. }
  10.  
  11. setInterval('dhtml()', 1000);
  12. dhtml();
  13. // clearInterval(id);
  14.  
AND
[HTML]
<html>
<head><script type="text/javascript" src="script.js"></script></head>
<body onload="" id="body">
<table border='1' color='black'>
<tr align='center' bgcolor='grey'>
<td colspan='300'>
<font size='10' color='red'><b><span id="container"></span></b></font>
</td>
</tr>
</table>
</body>
</html>
[/HTML]
Better?
Jan 8 '07 #2
Pete90
12
You're reloading the page every time so you reinitialize everything. You want to be doing something like this:
Expand|Select|Wrap|Line Numbers
  1. var i=0;
  2. var text=["Chocolate", "Vanilla", "Strawberry"];
  3.  
  4. function dhtml(){
  5.     document.getElementById('container').innerHTML = text[i];
  6.     i++;
  7.     if(i>2)
  8.         i=0;
  9. }
  10.  
  11. setInterval('dhtml()', 1000);
  12. dhtml();
  13. // clearInterval(id);
  14.  
AND
[HTML]
<html>
<head><script type="text/javascript" src="script.js"></script></head>
<body onload="" id="body">
<table border='1' color='black'>
<tr align='center' bgcolor='grey'>
<td colspan='300'>
<font size='10' color='red'><b><span id="container"></span></b></font>
</td>
</tr>
</table>
</body>
</html>
[/HTML]
Better?
Thanks, you are right... the page is reloading everytime. Including the function body onload="dhtml()" , it works! I notice that you have included the sentence <script type="text/javascript" src="script.js"></script> although <script language= "javascript"> seem to work fine too... i have always wonder what is the difference between them?
Jan 9 '07 #3
acoder
16,027 Expert Mod 8TB
I notice that you have included the sentence <script type="text/javascript" src="script.js"></script> although <script language= "javascript"> seem to work fine too... i have always wonder what is the difference between them?
Expand|Select|Wrap|Line Numbers
  1. src="script.js"
loads a javascript file called script.js whilst
Expand|Select|Wrap|Line Numbers
  1. <script language= "javascript">
actually contains javascript code within the tags on the actual page, like so:
Expand|Select|Wrap|Line Numbers
  1. <script language= "javascript">
  2. <!--
  3. //javascript code goes here
  4. //-->
  5. </script>
Jan 9 '07 #4
b1randon
171 Expert 100+
Expand|Select|Wrap|Line Numbers
  1. src="script.js"
loads a javascript file called script.js whilst
Expand|Select|Wrap|Line Numbers
  1. <script language= "javascript">
actually contains javascript code within the tags on the actual page, like so:
Expand|Select|Wrap|Line Numbers
  1. <script language= "javascript">
  2. <!--
  3. //javascript code goes here
  4. //-->
  5. </script>
Yup ^^ Thanks for the explanation, acoder.
Jan 9 '07 #5

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

Similar topics

8
by: Jukka K. Korpela | last post by:
I just noticed that most browsers render <table border="1"><tr><td><p>foo</p></td></tr></table> the same way as <table border="1"><tr><td>foo</td></tr></table> That is, they ignore the p...
1
by: Ben | last post by:
Hi all, I'm trying to write inside a table cell from external javascript but am not successful. When I insert inside a form within <td...>, it works but does not work for normal table cell. My...
10
by: evanburen | last post by:
I'm trying to text the text inside id="DirectorsCaption" within the hideDirectors() and showDirectors() functions but it's not working. I'm trying different variations using .innerHTML but with no...
1
nirmalsingh
by: nirmalsingh | last post by:
how to place controls inside table? i wrote code like <td align=left valign=middle class=formleftheading><inptut type=text value= " + hai" ></td> but it does'nt works
2
by: amitp | last post by:
hi i'm using MS Word2003 for my VB application which is a report. The word documents contains tables and the cells inside the table contain some fields. My application replaces the field values with...
36
by: karen987 | last post by:
My newsweblog has ASP pages with news articles and a commenting system. The comments are posted, and to read them the reader clicks on the headline of the comment, and it opens up in a pop up window....
6
by: BlackMustard | last post by:
hi, i'm using the following code to modify a standard word document and save it to disk with a new file name. Sub CreateQuote()...
3
by: cdm2883 | last post by:
i am having trouble put my text by side the links, I want my text here But my text is here!
8
by: James Kimble | last post by:
Yeah I'm sure this is a stupid question. I've got a javascript source file with functions (creates a bar graph) I want to call from inside an html table cell so that the graph appears in the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.