如何使用Javascript在HTML中增加数字? [英] How to make a number increase live in HTML using Javascript?

查看:335
本文介绍了如何使用Javascript在HTML中增加数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在制作一个网站来惹恼我的朋友,并且有一个下载页面。我想要一个伪下载计数器,它说:下载(var)次!



我想知道如何在指定的延迟后以毫秒为单位添加数字。



例如,它会

So I'm making a website to annoy my friend and there is a download page. I would like a pseudo download counter, where it says: Downloaded (var) times!

I was wondering how to add numbers to the value after a specified delay in milliseconds.

For example, it would go

Downloaded 1 times!



然后var将变为2(如果它每毫秒增加1),依此类推,但仅在指定的延迟时间后增加。



请不要使用增量,因为我可能希望能够更改增加的值以及延迟(以毫秒为单位)。



请在设置延迟时间的代码中添加注释,增加值的位置为。


Then the var would change to 2 (if it increased by 1 per amount of milliseconds) and so on, but only increased after the specified delay time.

Please refrain from using increment, because I might want to be able to change the value it increases by as well as the delay in milliseconds.

Please add comments to the code where the delay time is set, and where the value it increases by is.

推荐答案

没关系,我找到了答案。我会在这里写给我和其他人的参考:



< p>已下载< span id =number> 0< / span>时间!< / p>



< script type =text / javascript>

var i = 0; //值从零开始

函数增量(){

i ++; //更改为i + = [yourvalue]以获得除1以外的任何增加
document.getElementById('number')。innerHTML = i;

}

setInterval('increment()',2000); //每2秒递增

< / script>
Nevermind, I found the answer. I'll write it here for my and anyone else's reference:

<p>Downloaded <span id="number">0</span> times!</p>

<script type="text/javascript">
var i = 0; // Value starts off at zero
function increment() {
i++; // Change to i += [yourvalue] for any increase other than 1
document.getElementById('number').innerHTML = i;
}
setInterval('increment()', 2000); // Increments every 2 seconds
</script>


这篇关于如何使用Javascript在HTML中增加数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆