如何创建 JQuery 时钟/计时器

编程入门 行业动态 更新时间:2024-10-24 08:29:46
本文介绍了如何创建 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屋!

更多推荐

[db:关键词]

本文发布于:2023-03-28 18:48:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/737041.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:计时器   时钟   JQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!