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

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

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 1577
Dormilich
8,658 Expert Mod 8TB
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
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 Expert Mod 8TB
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
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 Expert Mod 8TB
@agbb0cg: out of interest, can you code that also without using globals?
Jul 29 '10 #6
@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.innerHTML = !numObj.innerHTML ? 0 : parseFloat(numObj.innerHTML) + 1;

but in this case, you would have a blank space the first 7 secs
Jul 29 '10 #7
Dormilich
8,658 Expert Mod 8TB
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
@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
@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
Dormilich
8,658 Expert Mod 8TB
I don't understand your last reply.
first I should mention that I omitted the definition for .bind() (it’s like a toolbox, there are functions, you always include, because they are damn useful).

the code would be:
Expand|Select|Wrap|Line Numbers
  1. Function.prototype.bind = function (obj) {
  2.     var fn = this;
  3.     return function () {
  4.         return fn.apply(obj, arguments);
  5.     }
  6. };
OK, the prerequisite (between agbb0cg and myself) was
- no global variables.

thus I created an anonymous function (function () {}) which I immediately executed (by appending ();). any locally defined variables (numb, update) are not global (well, stands to reason).

the code, how to increment the number is the same for both of us (there’s not much choice, if you don’t use a Closure).

the key difference in our update implementations, is how we access the element <p>. obviously, it is to get by ID. agbb0cg chose to use a DOM access each time the function executes. I chose to get the element once and bind this reference to the this in my update function (that’s where I use the (not so obvious) Closure from .bind()).

Please explain how it can be used.
as it stands there, the only thing to make sure is waiting for the <p> to be created (i.e. execute it at the end of the HTML)
Jul 29 '10 #11
@Dormilich
Your approach is fine! In fact, I'd probably go with yours in terms of performance, just for saying something...

I just thought that sometimes it's better to teach JS the basic way, the "pure-way" and also not just giving the answer to a problem.
I think my version it's easier to understand, edit and implement for a person who's asking how to do something like this, all of us have been there before...

Regards.
Jul 29 '10 #12
Dormilich
8,658 Expert Mod 8TB
as you had given a working solution, my code was intended for you. (I am well aware, the code was way beyond a beginner’s understanding, though you didn’t seem to be a beginner)
Jul 29 '10 #13
Code war!

;)
Jul 29 '10 #14
Dormilich
8,658 Expert Mod 8TB
;) .
Jul 29 '10 #15
Well, I have changed some of the code and now I have some working stuff.

"people have" will be changed to "person has" in this one. However, I cannot find a way to style the text. Any solutions?
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.   <body>
  3.     <p>
  4.         <span id="numb">0 people have died of hunger since you entered this webpage.</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 == 0 ? ' 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>
For this one, I can style it but "people have" will not be changed.
Expand|Select|Wrap|Line Numbers
  1. <html> 
  2.   <body> 
  3.     <nobr id="numb" style="color:green;">0</nobr>    <script type="text/javascript"> 
  4. document.write("<span style=\"color:green;\">people have died of hunger since you entered this webpage.<\/span>");
  5.  
  6.       setInterval (function(){ 
  7.         var numObj = document.getElementById('numb') 
  8.         numObj.innerHTML = parseFloat(numObj.innerHTML) + 1; 
  9.         i++; 
  10.       }, 7000); 
  11.     </script> 
  12.   </body> 
  13. </html> 
Thanks a lot for helping.
Jul 29 '10 #16
Dormilich
8,658 Expert Mod 8TB
removing some overheat.
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.   <body>
  3.     <p>
  4.         <span id="numb"></span>
  5.         <span id="txt"></span>
  6.         died of hunger since you entered this webpage.
  7.     </p>
  8.  
  9.     <script type="text/javascript">
  10.       setInterval (function(){
  11.         var numObj = document.getElementById('numb');
  12.         var txtObj = document.getElementById('txt');
  13.         var currentNum = parseFloat(numObj.innerHTML);
  14.  
  15.         numObj.innerHTML = currentNum + 1;
  16.  
  17.         var numTxt = currentNum == 1 ? ' person has' : ' people have'; 
  18.         txtObj.innerHTML = numTxt;
  19.       }, 7000);
  20.     </script>
  21.   </body>
  22. </html>
  23.  
Jul 29 '10 #17
Dormilich
8,658 Expert Mod 8TB
just another idea
Expand|Select|Wrap|Line Numbers
  1. (function () {
  2.     var numb = document.getElementById("numb");
  3.     numb.count = 0;
  4.     function update() {
  5.         this.innerHTML = this.count++;
  6.     }
  7.     setInterval(update.bind(numb), 7000);
  8. })();
Jul 29 '10 #18

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

Similar topics

43
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...
2
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...
32
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...
1
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"...
8
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 ...
2
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...
5
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
by: manchin2 | last post by:
Hi, Can anybody please provide the information about "&quot" and its use, if possible please provide an example. ...
4
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": ...
2
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? ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...

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.