473,672 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I increase "i" by 1 every 7 seconds? Look below.

4 New Member
I need help for some javascript code. I need to make it so that every 7 seconds, "i" will increase by 1. So that I will get " 'i' people have died from hunger since you entered this web page." Eg.
Start: 0 people have died from hunger since you entered this web page
After 7 seconds: 1 people have died from hunger since you entered this web page.
After another 7 seconds: 2 people have died from hunger since you entered this web page.
Please help!. This is what I have been doing so far. I don't really know what I am doing actually. It would be good if a If statement could be added such that for the case of i=1, "people have" will be changed to "person has"


Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3.  
  4. <script type="text/javascript">
  5. i=0
  6. setInterval ("i++", 7000 );
  7.  
  8. document.write(i);
  9.  
  10. function updateNumb(){
  11.     $('#numb').load();
  12. }
  13. setInterval( "updateNumb()", 7000 );
  14. </script>
  15. <p id="#numb">i</p>
  16.  
  17. </body>
  18. </html>
Thanks in advance!
Jul 29 '10 #1
17 1595
Dormilich
8,658 Recognized Expert Moderator Expert
ok, document.write( ) must not be used.

you have one setInterval() too many (you only need one process to repeat)

you didn’t include jQuery/prototype (whatever framework you try to use).

an id must not start with a hash (#), that’s a "reserved keyword" in HTML.
Jul 29 '10 #2
bbkc22113
4 New Member
Ok...Lets change it a bit

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3.  
  4. <script type="text/javascript">
  5. i=0
  6. setInterval ("i++", 7000 );
  7.  
  8. </script>
  9. <p id="numb">i</p>
  10.  
  11. </body>
  12. </html>
So I have to change the "i" at #numb so that it is a variable and then I have to make it change every 7 seconds.
Jul 29 '10 #3
Dormilich
8,658 Recognized Expert Moderator Expert
well, wrong setInterval() deleted, however, the logic is correcter this time. what you need to do as next step is getting the <p> to update (try that first without setInterval())

personal stickie:
Expand|Select|Wrap|Line Numbers
  1. $(#numb).update = function () {
  2.     setTimeout(this.update.bind(this))
  3. }
vs.
Expand|Select|Wrap|Line Numbers
  1. setInterval(update.bind($(#numb)))
Jul 29 '10 #4
agbb0cg
8 New Member
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.   <body>
  3.     <p id="numb"></p>
  4.  
  5.     <script type="text/javascript">
  6.       var i = 0;
  7.       setInterval (function(){
  8.         document.getElementById('numb').innerHTML = i;
  9.         i++;
  10.       }, 7000);
  11.     </script>
  12.   </body>
  13. </html>
Jul 29 '10 #5
Dormilich
8,658 Recognized Expert Moderator Expert
@agbb0cg: out of interest, can you code that also without using globals?
Jul 29 '10 #6
agbb0cg
8 New Member
@Dormilich
maybe something like this?:


Expand|Select|Wrap|Line Numbers
  1. <html>
  2.   <body>
  3.     <p id="numb">0</p>
  4.  
  5.     <script type="text/javascript">
  6.       setInterval (function(){
  7.         var numObj = document.getElementById('numb')
  8.         numObj.innerHTML = parseFloat(numObj.innerHTML) + 1;
  9.       }, 7000);
  10.     </script>
  11.   </body>
  12. </html>
  13.  

or you could have
<p id="numb"></p>

and

numObj.innerHTM L = !numObj.innerHT ML ? 0 : parseFloat(numO bj.innerHTML) + 1;

but in this case, you would have a blank space the first 7 secs
Jul 29 '10 #7
Dormilich
8,658 Recognized Expert Moderator Expert
I came up with this
Expand|Select|Wrap|Line Numbers
  1. (function () {
  2.     var numb = document.getElementById("numb");
  3.     function update() {
  4.         this.innerHTML = parseFloat(this.innerHTML) + 1;
  5.     }
  6.     setInterval(update.bind(numb), 7000);
  7. })();
Jul 29 '10 #8
bbkc22113
4 New Member
@agbb0cg Thanks a lot. Yours worked! But I still have to add " people have died of hunger since you entered this webpage." after the "0/1/2 etc." Eg. 3 people have died of hunger since you entered this webpage. I need to make it such that there are no breaks. If possible, please try to add a If statement such that if it is 1, then it will say "person has" instead of "people have", but it is not that necessary.
@Dormilich I don't understand your last reply. Please explain how it can be used. Thanks a lot.
Jul 29 '10 #9
agbb0cg
8 New Member
@bbkc22113
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.   <body>
  3.     <p>
  4.         <span id="numb"></span>
  5.         <span id="txt"></span>
  6.     </p>
  7.  
  8.     <script type="text/javascript">
  9.       setInterval (function(){
  10.         var numObj = document.getElementById('numb');
  11.         var txtObj = document.getElementById('txt');
  12.         var currentNum = parseFloat(numObj.innerHTML);
  13.  
  14.         numObj.innerHTML = currentNum + 1;
  15.  
  16.         var numTxt = currentNum == 1 ? ' person has' : ' people have';
  17.         numTxt += ' died of hunger since you entered this webpage.';
  18.  
  19.         txtObj.innerHTML = numTxt;
  20.         i++;
  21.       }, 7000);
  22.     </script>
  23.   </body>
  24. </html>
Jul 29 '10 #10

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

Similar topics

43
5092
by: steve | last post by:
I am quite frustrated with php’s include, as I have spent a ton of time on it already... anyone can tell me why it was designed like this (or something I don’t get)? The path in include is relative NOT to the immediate script that is including it, but is relative to the top-level calling script. In practice, this means that you have to constantly worry and adjust paths in includes, based on the startup scripts that call these...
2
2932
by: steve | last post by:
Hi, I need to do conditional script "include", but like to pull the code from db instead of a file. How do I do that? Reason: I like to implement some complex regex logic, and make it table driven. The regex would include if/then/else type logic, and would like my script to conditionally execute the logic. -- http://www.dbForumz.com/ This article was posted by author's request
32
4129
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open Source" manner, and in an attempt to build a ground-swell of support to convince the folks at Microsoft to add it. Proposal: "first:" "last:" sections in a "foreach" block The problem: The foreach statement allows iterating over all the...
1
6478
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting" setting to be "E_ALL", notices are still not getting reported. The perms on my file are 664, with owner root and group root. The php.ini file is located at /usr/local/lib/php/php.ini. Any ideas why the setting does not seem to be having an effect? ...
8
3611
by: Ulysse | last post by:
Hello, I need to clean the string like this : string = """ bonne mentalit&eacute; mec!:) \n <br>bon pour info moi je suis un serial posteur arceleur dictateur ^^* \n <br>mais pour avoir des resultats probant il faut pas faire les mariolles, comme le &quot;fondateur&quot; de bvs
2
3987
by: Angus | last post by:
I am trying to change the selection in Javascript - but this HTML element is not a standard option control. On the web page it looks like a dropdown list - and you click on the right hand down arrow and you see the entries in a dropdown. But how would I control this thing from javascript? Here is the HTML: <div class="selection" style="top:0; left:89; width:159; height:21;"
5
3683
by: Maria Sudderman | last post by:
I have a prblem with the "onClick" command. onClick="insert('<a href="URI">', '</a>')"> but this is not correct! why? Maria
1
2836
by: manchin2 | last post by:
Hi, Can anybody please provide the information about "&quot" and its use, if possible please provide an example. 1)<tm:bom-expression>{Conf.getEquityConfLookupFields().getEventFieldText(&quot;AdditionalDisruption&quot;,&quot;Change in Law&quot;)}</tm:bom-expression> 2)07:41:08 Default ( call ( . ( call ( . Conf getEquityConfLookupFields ) ) getEventFieldText ) ( , AdditionalDisruption Change inLaw ) ) value=Not applicable Can you please...
4
4206
by: thaytu888888 | last post by:
Here is my codes in aspx page: <td colspan="2" class="main_menu" runat="server" onclick='toggleDisplay(<%#Eval("description")%>);'><%#Eval("description")%></td> Here is in "View source": onclick="toggleDisplay(&lt;%#Eval(&quot;description&quot;)%>);">Administrator Functions</td> When putting <%#Eval('description')%> in <td> tag, it understand & show the correct value, but when in onclick='toggleDisplay(<%#Eval("description")%>);'> it didn't...
2
37700
by: jmash | last post by:
Suppose I have the following string whch is part of an xml string: String s= "Script Id=&quot;Test&quot; " And I need to get s= "Script Id="Test" " Can anyone tell me how this can acheived? Thanks, jmash
0
8485
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
8403
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8930
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
8828
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
6238
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4417
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2819
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
2
2062
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.