如何创建 JQuery 时钟/计时器 [英] How to create a JQuery Clock / Timer

查看:30
本文介绍了如何创建 JQuery 时钟/计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的测验应用程序,我想在页面顶部显示一个漂亮的计时器/时钟,向用户显示他们已经走了多长时间.(如果我能以某种方式向他们展示 Total Quiz Time 的计时器,以及 This Question Time 的第二个计时器,那就更酷了,但是一旦我有一个计时器工作,我应该能够弄清楚如何做到这一点.

我的问题是:

使用 JQuery 显示一个简单的计时器/时钟的好方法是什么?(直接JS也可以)我知道如何查看时间,但是如何获得递增的秒数?

我自己的搜索一直引导我使用 JQuery 插件(我想推出自己的插件)以及事件计时器",这不是我想要的......

解决方案

您正在寻找 setInterval 函数,它每 x 毫秒运行一个函数.

例如:

var start = new Date;设置间隔(函数(){$('.Timer').text((new Date - start)/1000 + "秒");}, 1000);

I have a simple quiz application and I want display a nice timer / clock at the top of the page which shows the user how long they've been going for. (If I could somehow show them a timer for Total Quiz Time and also a second one for This Question Time that would be even cooler but I should be able to figure out how to do myself that once I've got one timer working.

My question is:

What's a nice, easy way to show a simple timer / clock using JQuery? (straight JS is also ok) I know how to check time, but how do I get incrementing seconds?

My own searches keep leading me to JQuery plugins (I want to roll my own) and also "event timers" which are not what I'm looking for...

解决方案

You're looking for the setInterval function, which runs a function every x milliseconds.

For example:

var start = new Date;

setInterval(function() {
    $('.Timer').text((new Date - start) / 1000 + " Seconds");
}, 1000);

这篇关于如何创建 JQuery 时钟/计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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