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" - <html>
-
<body>
-
-
<script type="text/javascript">
-
i=0
-
setInterval ("i++", 7000 );
-
-
document.write(i);
-
-
function updateNumb(){
-
$('#numb').load();
-
}
-
setInterval( "updateNumb()", 7000 );
-
</script>
-
<p id="#numb">i</p>
-
-
</body>
-
</html>
Thanks in advance!
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.
Ok...Lets change it a bit - <html>
-
<body>
-
-
<script type="text/javascript">
-
i=0
-
setInterval ("i++", 7000 );
-
-
</script>
-
<p id="numb">i</p>
-
-
</body>
-
</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.
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: - $(#numb).update = function () {
-
setTimeout(this.update.bind(this))
-
}
vs. - setInterval(update.bind($(#numb)))
- <html>
-
<body>
-
<p id="numb"></p>
-
-
<script type="text/javascript">
-
var i = 0;
-
setInterval (function(){
-
document.getElementById('numb').innerHTML = i;
-
i++;
-
}, 7000);
-
</script>
-
</body>
-
</html>
Dormilich 8,658
Recognized Expert Moderator Expert
@agbb0cg: out of interest, can you code that also without using globals?
@Dormilich
maybe something like this?: - <html>
-
<body>
-
<p id="numb">0</p>
-
-
<script type="text/javascript">
-
setInterval (function(){
-
var numObj = document.getElementById('numb')
-
numObj.innerHTML = parseFloat(numObj.innerHTML) + 1;
-
}, 7000);
-
</script>
-
</body>
-
</html>
-
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
Dormilich 8,658
Recognized Expert Moderator Expert
I came up with this - (function () {
-
var numb = document.getElementById("numb");
-
function update() {
-
this.innerHTML = parseFloat(this.innerHTML) + 1;
-
}
-
setInterval(update.bind(numb), 7000);
-
})();
@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.
@bbkc22113 - <html>
-
<body>
-
<p>
-
<span id="numb"></span>
-
<span id="txt"></span>
-
</p>
-
-
<script type="text/javascript">
-
setInterval (function(){
-
var numObj = document.getElementById('numb');
-
var txtObj = document.getElementById('txt');
-
var currentNum = parseFloat(numObj.innerHTML);
-
-
numObj.innerHTML = currentNum + 1;
-
-
var numTxt = currentNum == 1 ? ' person has' : ' people have';
-
numTxt += ' died of hunger since you entered this webpage.';
-
-
txtObj.innerHTML = numTxt;
-
i++;
-
}, 7000);
-
</script>
-
</body>
-
</html>
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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
|
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...
|
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? ...
|
by: Ulysse |
last post by:
Hello,
I need to clean the string like this :
string =
"""
bonne mentalité 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 "fondateur" de bvs
| |
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;"
|
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
|
by: manchin2 |
last post by:
Hi,
Can anybody please provide the information about """ and its use, if possible please provide an example.
1)<tm:bom-expression>{Conf.getEquityConfLookupFields().getEventFieldText("AdditionalDisruption","Change in Law")}</tm:bom-expression>
2)07:41:08 Default ( call ( . ( call ( . Conf getEquityConfLookupFields ) ) getEventFieldText ) ( , AdditionalDisruption Change inLaw ) ) value=Not applicable
Can you please...
|
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(<%#Eval("description")%>);">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...
|
by: jmash |
last post by:
Suppose I have the following string whch is part of an xml string:
String s= "Script Id="Test" "
And I need to get
s= "Script Id="Test" "
Can anyone tell me how this can acheived?
Thanks,
jmash
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |