473,795 Members | 3,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

setInterval problems?

12 New Member
Hi,

I cobbled together this code to give me continuously changing text from one of (currently) two defined arrays. Needless to say I know nothing at all about Javascript and so, when things go wrong, I need help finding out why, and what to do about it...

The code itself actually works fine, but when I try it in w3schools' TryIt Editor a message in the status bar saying "Error on page" although, practically speaking, there appears to be no problem.

Could anybody have a look through the following and tell me if there's anything I can do to make my status bar happy?

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2.  
  3. var m=0;
  4. var text=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"];
  5.  
  6. function dhtml(){
  7. document.getElementById('first').innerHTML = text[m];
  8. m++;
  9. if(m>10)
  10. m=0;
  11. }
  12.  
  13. setInterval('dhtml()', 250);
  14. dhtml();
  15. // clearInterval(id);
  16.  
  17. </script>
  18.  
  19. <script language="javascript">
  20.  
  21. var n=0;
  22. var two=["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"];
  23.  
  24. function any(){
  25. document.getElementById('second').innerHTML = two[n];
  26. n++;
  27. if(n>9)
  28. n=0;
  29. }
  30.  
  31. setInterval('any()', 2500);
  32. any();
  33. // clearInterval(id);
  34.  
  35. </script>
  36.  
  37. <html>
  38. <head>
  39. <table>
  40. <tr align='center' bgcolor='black' height=200px>
  41. <td align='center' width=200px>
  42. <font style='font-size:27; color:white; font-family:arial'><b>
  43. <script language="javascript" type="text/javascript">
  44. function random_text()
  45. {};
  46. var random_text = new random_text();
  47. var number = 0;
  48. random_text[number++] = "<span id=first>"
  49. random_text[number++] = "<span id=second>"
  50. var random_number = Math.floor(Math.random() * number);
  51. document.write(random_text[random_number])
  52. </script>
  53. </span></b></font>
  54. </td>
  55. </tr>
  56. </table> 
  57. </body>
  58. </html>
Thanks,

Hannah
Oct 23 '07 #1
4 2156
Ferris
101 New Member
Hi:

the problem is you just create only one span,look at your code

[HTML]
<font style='font-size:27; color:white; font-family:arial'>< b>
<script language="javas cript" type="text/javascript">
function random_text()
{};
var random_text = new random_text();
var number = 0;
random_text[number++] = "<span id=first>"
random_text[number++] = "<span id=second>"
var random_number = Math.floor(Math .random() * number);
document.write( random_text[random_number])
</script>

</span></b></font>
[/HTML]

you just create one span.which span?maybe "first" or maybe"second",i t depends on random_number,b ut only one created.

and look at here...


[HTML]
document.getEle mentById('first ').innerHTML = text[m];
......
document.getEle mentById('secon d').innerHTML = two[n];
[/HTML]

you want to print alpha in span "first",and digit in span "secode",bu t there's only one span,so,error occors.



I don't know why you define the empty function "random_text".a nd I also don't understand why you define two spans ,I think one span can work perfectly,so I changed the code as below:

[HTML]


<html>
<head>
</head>
<script language="javas cript">
var m=0;
var text=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"];
function dhtml(){
document.getEle mentById('out') .innerHTML = text[m];
m++;
if(m>10)
m=0;
}
setInterval('dh tml()', 250);
//dhtml();
// clearInterval(i d);
</script>

<script language="javas cript">
var n=0;
var two=["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"];
function any(){
document.getEle mentById('out') .innerHTML = two[n];
n++;
if(n>9)
n=0;
}
setInterval('an y()', 2500);
//any();
// clearInterval(i d);
</script>
<body>
<table>
<tr align='center' bgcolor='black' height=200px>
<td align='center' width=200px>
<font style='font-size:27; color:white; font-family:arial'>< b>
<span id="out"></span>
</b></font>
</td>
</tr>
</table>
</body>
</html>
[/HTML]

there is no error occurs now,and you'll have a happy status bar.



by the way,look at the code:
setInterval('dh tml()', 250);
dhtml();

setInterval('dh tml()', 250); means js will call function "dhtml" every 250ms,so you needn't to write dhtml(); to call it again.


I wish it will help you.
Oct 23 '07 #2
Schannah
12 New Member
The reason I create two under different names is so that I can write a code that randomly picks one of them. I don't know another way of doing this, and the code you've written -- giving the two different spans the same name -- means that the same one gets selected each time. Thank you for the tips, though; I will remove the redundant parts from the code.

I've looked more closely at what happens when I use the code and the error message comes up in at two different times, depending on which span is selected:

-- If the chain 1, 2, 3 etc. gets selected, the error message comes up straight away although it still functions;
-- If the chain a, b, c etc. gets selected, the error message comes up after it has been through one cycle and reset to m=0.

Does this help identify the problem at all?
Oct 23 '07 #3
Ferris
101 New Member
Hi

-- If the chain 1, 2, 3 etc. gets selected, the error message comes up straight away although it still functions;
-- If the chain a, b, c etc. gets selected, the error message comes up after it has been through one cycle and reset to m=0.
this is because of these code:

setInterval('dh tml()', 250);
setInterval('an y()', 2500);

you print out a,b,c every 250ms,but 1,2,3 is printed out every 2500ms,so a,b,c will be printed before 1,2,3.
if the span "second" is created,the error orrurs soon,because a,b,c has no span to be printed out.
if the span "first" is created,the error message will not comes up soon,because 1,2,3 will not be printed soon.when m =10,it means 250*10ms has gone, then 1 want to be printed out,but there's no span for 1,2,3 to print out.

you can change setInterval('an y()', 2500); into setInterval('an y()', 250); to see the difference.



if you want to randomly picking up one of two spans,you should ensure there're two spans exsisted,or you will get an error message.
you can surely create two different spans for your purpose,and you can hide one of them when you want to print.

here's the new code:

[HTML]
<html>
<head>
</head>
<script language="javas cript">
var m=0;
var text=["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"];
function dhtml(){
document.getEle mentById('first ').innerHTML = text[m];
document.getEle mentById('first ').style.displa y = "";
document.getEle mentById('secon d').style.displ ay = "none";
m++;
if(m>10)
m=0;
}
setInterval('dh tml()', 250);
//dhtml();
// clearInterval(i d);
</script>

<script language="javas cript">
var n=0;
var two=["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"];
function any(){
document.getEle mentById('secon d').innerHTML = two[n];
document.getEle mentById('secon d').style.displ ay = "";
document.getEle mentById('first ').style.displa y = "none";
n++;
if(n>9)
n=0;
}
setInterval('an y()', 2500);
//any();
// clearInterval(i d);
</script>
<body>
<table>
<tr align='center' bgcolor='black' height=200px>
<td align='center' width=200px>
<font style='font-size:27; color:white; font-family:arial'>< b>
<span id="first"></span>
<span id="second"></span>
</b></font>
</td>
</tr>
</table>
</body>
</html>
[/HTML]
Oct 23 '07 #4
Schannah
12 New Member
The problem is that I want one span to be selected at random. This is because I am putting it in a page that uses templates in editing; an example of what I mean is:

Main Layout:

(variable to display a set of boxes, e.g. of links, extra information, etc.)

Box layout

(html to control the appearance of all such boxes)

and so on. So I can define what I want each box to look like but if I want to put in something different for each box, which I do, the only way I can do it is to create a random feature generator in the box layout. So the point is that under each box, for example, I want to put this code that displays a randomly selected chain of text that I will have defined, like a-b-c-d and so on, so that one box may have a-b-c and one box may have 1-2-3 underneath it. Does this help you understand why I need the function to choose a particular span at random?
Oct 26 '07 #5

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

Similar topics

10
7797
by: Richard A. DeVenezia | last post by:
At line this.timerId = setInterval (function(){this.step()}, 100) I get error dialog Error:Object doesn't support this property or method. If I change it to this.timerId = setInterval (function(){this==window}, 100) I see true, the sad fact that 'this' is window and not an anim. What are some proper ways ? I would like to avoid
17
2135
by: George Hester | last post by:
Hoe can I use setInterval where its argument is a function which also has an argument? For example I have a function change(Msg) where Msg is dynamically generated and returns nothing at this point (other than true I guess). I would like to have setInterval(change(Msg)); but that doesn't work. Any suggestions? Thanks. -- George Hester __________________________________
3
1700
by: Richie | last post by:
Is there a way to access an Event object associated with setInterval()? I'm using Mozilla and Firefox on various platforms.
6
15229
by: marktm | last post by:
Hi- I am trying to use setInterval to call a method within an object and have not had any luck. I get "Object doesn't support this property or method" when I execute the following code. What I am doing wrong? Your help will be much appreciated. Thanks
3
5815
by: shawn | last post by:
Hi All Was trying to get this bit of code working (simplified of course) for (i=0;i<uuid_num;i++) { window.setInterval(InitMap(uuidValues), timePeriod); } Where uuid_num, uuidValues, timePeriod are defined above, along with function InitMap.
1
1972
by: Yansky | last post by:
Hi, I'm having some problems getting this code to work in IE. http://pastebin.ca/454033 It's giving me an error of "Object Expected" in IE. The first ahah("sidebar-threadwatch.cfm?side=yes.htm"); ajax call works just fine, so I think it is window.setInterval('ahah("sidebar- threadwatch.cfm?side=yes.htm")', 60000); that's producing the error. It's working fine in FF though, so I'm not sure what's causing it to
1
2404
by: Terry | last post by:
Hi folks. I tried to use setInterval like this but it did not work setInterval ( removeitem (theitem) { theitem.style.display= "none"; }, 1000 ); the function without the setInterval is
1
2513
by: jonahmason | last post by:
hi. a javascript newbie here in dire need of help. for the past 3 days i've been trying to get a script using setinterval to work but a part of the code, that executes after setinterval hits a certain amount of time and clearinterval is invoked, opens on a new page instead of displaying in the designated div. i've tried 2 ways of displaying the code 1. getelementbyid('designatedarea').innerhtml and 2. var x...
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10448
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10167
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6784
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5566
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2922
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.