- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为了简单起见,假设我必须构建一个不断更新并显示每秒出生多少只小狗、小猫和老鼠的页面。就像计数器或计时器一样。
我还必须显示自早上 6 点以来和本月初以来出生了多少只小狗、小猫和老鼠。
我不知道如何使用 Date() 确定自早上 6 点以来以及自当月之星以来出生了多少只小狗、小猫和老鼠,并不断更新该数量。
HTML
<section id="s01">
<h1>if 3 puppies are born every second</h1>
<b id="badge01" class="now">0</b> puppies have been born so far.<br>
<b class="morning">?</b> puppies have been born since this 6 a.m. morning.<br>
<b class="month">?</b> puppies have been born since the first of this month.
</section>
<section id="s02">
<h1>if 5 kittens are born every second</h1>
<b id="badge02" class="now">0</b> kittens have been born so far.<br>
<b class="morning">?</b> kittens have been born since this 6 a.m. morning.<br>
<b class="month">?</b> kittens have been born since the first of this month.
</section>
<section id="s03">
<h1>if 7 rats are born every second</h1>
<b id="badge03" class="now">0</b> rats have been born so far.<br>
<b class="morning">?</b> rats have been born since this 6 a.m. morning.<br>
<b class="month">?</b> rats have been born since the first of this month.
</section>
JS
var now = new Date();
var dayOfMonth = now.getDate();
setInterval(function () {
// 3 puppies born every second
$('#badge01').html(parseInt($('#badge01').html()) + 3);
// 5 kittens born every second
$('#badge02').html(parseInt($('#badge02').html()) + 5);
// 7 rats born every second
$('#badge03').html(parseInt($('#badge03').html()) + 7);
}, 1);
我的 JSFiddle 在这里 - http://jsfiddle.net/gJy4x/6/ (我是 JS 新手,所以请耐心等待。)
最佳答案
首先,setInterval()
的第二个参数以毫秒为单位。因此您希望该值为 1000,而不是 1。
假设每秒有 5 只小猫(我喜欢小猫!)。
今天早上 6 点以来有多少只小猫出生?
// Cache date object
var now = new Date();
// Give me milliseconds!
var six = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 6);
// Time now
var timeNow = now.getTime();
// Convert milliseconds to seconds
// Since it's in floating point, we'll round it.
var difference = Math.round((timeNow - six) / 1000);
// Woot, kittens!
var totalKittens = difference * 5;
本月初以来出生了多少只小猫?
同样的想法!我们将使用:
,而不是使用6
var monthBegin = new Date(now.getFullYear(), now.getMonth());
您可以省略这一天。暗示了月初。我怎么知道呢?
在文档中:
If at least two arguments are supplied, missing arguments are either set to 1 (if date is missing) or 0 for all others.
您应该了解有关 Date object 的更多信息.
关于javascript - 如何每秒将变量增加 (x) 个单位,然后将变量与 Date() 一起使用来确定自月初以来的单位数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20256126/
我想要一个按钮,单击该按钮会在指定时间后刷新当前页面。 我目前有: setTimeout(function reload(){ location = '' },1000) Relo
我有一个场景,我需要每秒向服务器发送数据(非常少的字节)。这个细节很重要,所以我也需要对其进行加密。 深入研究后,我认为我们可以使用 HTTP 协议(protocol),但在这种情况下,HTTP he
我在即将进行的测试的学习指南中得到了这个问题。我不明白如何解决这个问题。今天是我的考试,我希望能得到一些帮助。 如果 CPU 每条指令发出一个内存请求并且计算机以 200 MIPS 运行,大约需要多少
我正在编写一个程序来确定每秒可以运行多少个 NOP,但我得到的数字似乎非常小。 int main() { struct timeval tvStart, tvDiff, tvEnd;
我想实现每秒 5-1 百万次远程函数调用。假设我们有一台开始计算的 Central 计算机,还有一台执行计算的 Worker 计算机。实际配置中会有很多Worker计算机。 假设我们的任务是计算一个[
下面的代码向最后一个 div 添加了一个类: $(".mydivs:last").addClass('added'); 这适用于页面加载,但 div 在动画中,因此顺序会改变。有没有办法让代码每秒运行
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwa
我刚刚在 HBase 中创建了一个表并用数据填充了它。从 7 个区域服务器看来,数据已写入区域服务器 6 和 7。 但我不明白为什么服务器 6 和 7 的每秒请求数为零? 最佳答案 读取请求计数 和写
我正在开发一个聊天应用程序,需要您的帮助。如果有人在线,我将信息存储在数据库中。现在我有一个功能,可以显示您的好友列表以及聊天伙伴是否在线。问题是:如果有人上网,我想经常检查。到目前为止,我有一个每秒
我正在编写一个快速而肮脏的脚本来检测服务器停机时间。我有一台服务器和两个远程客户端,它们每秒对服务器执行一次 ping 操作以查看它是否已启动。 如果您最多可以每分钟运行一次 cron,我该如何设置任
有什么方法可以让 Graphite 绘制图表请求吗? 当你从 nginx_status 检索 nginx 请求时,你正在向 Graphite 发送一个绝对值,所以我在想是否有任何方法可以获取每秒的速率
我正在构建一个 Android 应用程序,它将加速度计和陀螺仪数据记录到文本文件中。在大多数教程中,他们使用的方法涉及创建两个文本文件,并每秒打开和关闭它们 50 次。即: private stati
嘿,我目前在我的 mysql 上有超过 300+ qps。在相当繁重的 PHP 网站上大约有 12000 UIP 一天/没有 cron。我知道在没有看到该网站的情况下很难判断是否可以,但您是否认为这完
我希望每秒钟使用我的鼠标移动功能旋转矩形我做错了什么?我怎样才能做我的变换,以便高度和旋转每隔一个元素改变一次?我用 if ( i % 2 == 0){} 试了一下 function modifyRe
有没有办法配置具有恒定负载的性能测试用例(例如,每秒 3 个新请求,持续 1 分钟)? 其他负载测试库具有此功能来设置请求率(例如 Artillery.io、Vegeta)。 k6 有设置VUs 的方
(只是想提一下,这是我的第一个问题,如果我做错了什么,我深表歉意)。我正在制作一个解析 CSV 文件并将其保存为列表的 Python 程序。但是,该程序需要用户输入他们希望将数据发送到服务器的速度。我
如何使用 python 将毫秒转换为帧数?我知道视频的帧速率(每帧 25 秒) 2683480 2684448 最佳答案 我最终决定手动执行此操作,因为我仍然没有弄清楚 ffmpeg 函数等 25fp
我有一个动态表,它按从主要编号到次要编号的降序排列。我想用 jQuery 在前 2 行放置红色背景,在接下来的行放置橙色背景,在接下来的 2 行放置黄色背景,在接下来的 3 行放置绿色背景。 表结构:
构建 Rails 应用程序 (ruby 2.4.0p0/Rails 5.1.4) 并使用我的 Macbook air (MacOS High Sierra 10.13.2) 在本地进行测试,但我一直遇
这个问题已经有答案了: NSTimer not firing when runloop is blocked (2 个回答) 已关闭 9 年前。 我使用 NSTimer,它每秒触发一次并更新一个标签,
我是一名优秀的程序员,十分优秀!