Javascript第二个柜台 [英] Javascript Second Counter

查看:88
本文介绍了Javascript第二个柜台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网站上,我正在尝试计算(并显示)用户在我的网站上停留了多少秒(而不是几分钟或几小时)。因此,如果他们已经使用了5分钟,它将显示300,而不是5分钟。
我对JavaScript没有经验,所以请帮忙。

On my website, I am trying to count (and display) how many seconds (not minutes or hours) the user has been on my site. So, if they have been on it for 5 minutes, it will display 300, Not 5 minutes. I am Very Unexperienced with JavaScript, So please help.

推荐答案

你可以使用 setInterval 函数可以根据您的选择频繁运行另一个函数。例如:

You can use the setInterval function to run another function as often as you choose. For example:

var seconds = 0;
var el = document.getElementById('seconds-counter');

function incrementSeconds() {
    seconds += 1;
    el.innerText = "You have been here for " + seconds + " seconds.";
}

var cancel = setInterval(incrementSeconds, 1000);

<div id='seconds-counter'> </div>

如果您运行此代码段,那么''我会看到柜台工作。

If you run this snippet, you'll see the counter working.

setInterval 函数有两个参数:


  • 您要拨打的功能

  • 通话之间的毫秒数

由于你想每秒调用递增计数器,你想使用1000毫秒(1秒)。

Since you want to call increment the counter every second, you want to use 1000 milliseconds (1 second).

有关详细信息,请参阅 setInterval的MDN文档

For more details, see the MDN documentation for setInterval.

这篇关于Javascript第二个柜台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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