- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经建立了一个简单的计时器,但我希望它尽可能快速和准确。换句话说,我想要最好的刷新率,同时保持它的精度,我的意思是秒的数字应该每 1000 毫秒改变一次,分钟应该每 60 秒改变一次,等等。
chronometer本身很容易制作,但是当我尝试快速制作时,我会出现奇怪的行为。
起初,计时器只是停留在 00:00,甚至不会改变。但我很快意识到这是因为我没有给浏览器任何时间来在每次执行更新和格式化时间的函数之间渲染元素。所以我试图做到这一点,以便每次下一次更新都在 0 毫秒超时后进行。这样,我认为它会将下一次更新的执行放在执行堆栈的末尾,这样浏览器就会在执行下一次更新函数之前渲染元素。
超时解决了我的问题,但只是部分解决了。计时器似乎工作正常,直到我意识到每隔一段时间就会跳过一秒。
我尝试在超时中添加一个 5 毫秒的小延迟,但仍然跳过了几秒钟,尽管每个跳过的秒之间的延迟更大。我尝试使用超时延迟,但即使延迟超过 50 毫秒,仍然会跳过几秒钟。我什至尝试使用 queueMicrotask
函数并将它与超时结合起来,但它没有帮助。
在这一点上,我试图找到另一个解决方案,但我找不到任何东西。我不明白为什么会发生这种情况,所以如果有人能向我解释发生了什么以及如何解决这个问题,我将不胜感激。
注:每个跳过的秒数之间的延迟不是恒定的,出于某种原因,1 分钟后会更频繁地跳过秒数。
计时器
const START_TIME = Date.now();
function formatTime(ms) {
ms = Math.round(ms * 1);
let secs = Math.trunc(ms / 1e3);
ms = (ms / 1e3 - secs) * 1e3;
let mins = Math.trunc(secs / 60);
secs = (secs / 60 - mins) * 60;
let hours = Math.trunc(mins / 60);
mins = (mins / 60 - hours) * 60;
hours = String(Math.trunc(hours));
mins = String(Math.trunc(mins));
secs = String(Math.trunc(secs));
ms = String(Math.trunc(ms));
const pad = (t, i = 2) => t.padStart(i, "0");
return `${pad(hours)}:${pad(mins)}:${pad(secs)},${pad(ms, 3)}`;
}
(() => {
const el = document.querySelector('.time');
const update = () => {
el.innerText = formatTime(Date.now() - START_TIME);
setTimeout(update);
}
update();
})();
@import url("https://fonts.googleapis.com/css2?family=Inconsolata:wght@300&display=swap");
body {
background-color: #222;
color: #35E;
margin: 0;
padding: 0;
}
.wrapper {
font-family: "Inconsolata", monospace;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
font-size: calc(4vw + 4vh + 10vmin);
height: 100vh;
width: 100vw;
display: flex;
justify-content: center;
align-items: center;
}
<div class="wrapper">
<div class="time"></div>
</div>
最佳答案
这是一个糟糕的数学 - 你正在累积错误。我没有在下面累积任何错误,因为我总是在 ms 上操作。
const START_TIME = Date.now();
function formatTime(diff) {
// ms are always the last three digits
const ms = ((diff / 1000 - Math.trunc(diff / 1000)) * 1000)
.toFixed(0)
.padStart(3, 0);
// seconds are always the integer part of the diff when divided by 1000 - mod 60 here the number "wraps" when it gets to 60
const seconds = (Math.trunc(diff / 1000) % 60).toFixed(0).padStart(2, 0);
// minutes are always the integer part of the diff when divided by 1000*60 - mod 60 again
const minutes = (Math.trunc(diff / (1000 * 60)) % 60)
.toFixed(0)
.padStart(2, 0);
// hours are the integer part of the diff when divided by 1000*60*60
const hours = Math.trunc(diff / (1000 * 60 * 60))
.toFixed(0)
.padStart(2, 0);
return `${hours}:${minutes}:${seconds}:${ms}`;
}
(() => {
const el = document.querySelector('.time');
const update = () => {
el.innerText = formatTime(Date.now() - START_TIME);
setTimeout(update);
}
update();
})();
@import url("https://fonts.googleapis.com/css2?family=Inconsolata:wght@300&display=swap");
body {
background-color: #222;
color: #35E;
margin: 0;
padding: 0;
}
.wrapper {
font-family: "Inconsolata", monospace;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
font-size: calc(4vw + 4vh + 10vmin);
height: 100vh;
width: 100vw;
display: flex;
justify-content: center;
align-items: center;
}
<div class="wrapper">
<div class="time"></div>
</div>
关于javascript - 高频DOM突变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63839057/
我想用 Graphite 烯一口气创造很多人。 文档只提到了这样创建一个人的方法: class CreatePerson(graphene.Mutation): class Input: na
我想发送没有子部分的 graphql 突变请求 mutation _ { updateCurrentUser(fullName: "Syava", email: "fake@gmail.com
通常查询是在您获取数据时,而变异是在您操作数据时。但是我将如何在没有任何参数的情况下实现突变? 在我的特殊情况下,我删除并创建了 2fa token 端点。 delete 和 create token
我正在使用 REDUX-TOOLKIT-QUERY,现在我有一种情况,我必须调用一个突变。一旦该突变返回响应,我必须在其他三个突变中使用该响应作为请求参数。我们如何才能以正确的方式实现它? const
突变是用于操作数据的查询。如果是这样,那么我的 root query和 root mutation树应该看起来相似吧?他们都应该允许嵌套字段(嵌套突变)。我正在玩这个(使用 express-graph
使用 R 3.6.1 (64) 位。使用 readxl 将数据框导入 R(命名为“RawShift”。我创建了 6 个变量(类:“字符”),它们是用户名列表。每个列表都以用户所在的团队命名。 我想使用
我正在尝试将 dplyr 中的 mutate 与动态变量名称一起使用。我发现了一些关于 SO(here、here 和 here)的帖子,它们让我更接近但不是可行的解决方案。我不认为缺少什么,但我需要你
我有一个现有的代码库,它使用 Meteor 方法接收来自客户端的请求并在服务器上执行数据库操作。我想尝试将此项目转移到 GraphQL,但我无法理解该工作的范围,特别是这对这些 Meteor 方法意味
我正在使用 Apollo Client 并尝试构建一个提交对象的突变,该对象的一部分是子类型的数组,该数组的大小需要是动态的,但我找不到任何有关如何正确构建此数组的文档。 这是我使用手动输入的数组进行
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 4 年前。 Improv
我的应用程序的目标是让用户能够保存和调用要填写和编辑的表单列表。一个用户会有多个表单,一个表单将由它自己的几个字段组成。 如果我的类型是这样设置的,例如: const FormType = new G
我刚刚写完这段代码: /// Watcher to look for changes in files pub struct Watch { glob:Option, ruv:Vec, p
我做了这个: test 1 为什么.b没有激活警报? $( ".a" ).click(function() { $('div').html('test 2'); }); $( ".b" ).click
我使用 graphene-django 来获得 GrapQL API。我在我的 schema.py 中创建了一个突变: class UpdateApplication(graphene.Mutatio
我正在使用适用于 Android 的 graphql apollo 客户端。我能够使用单个字段的突变传递数据而没有任何问题。在突变中使用输入对象时,发生错误无法读取未定义的电子邮件属性。但同样的 gr
我有两个变量,一个是可变数组,另一个是不可变的。 let mutableArray = NSMutableArray(array: ["1","2","3"]) let immutableArray:
是否可以从一个突变中对另一个 graphqlType 运行一个突变?我不确定这是否是个好主意。理想情况下,最好为每种 graphql 数据类型创建突变,然后在需要时调用这些函数中的每一个。希望这能让事
我需要能够在单个请求中创建一个用户并添加它最喜欢的电影(一个对象数组,引用了 Movies 集合和他对每部电影的个人评分)。 看起来像这样的东西(伪代码) var exSchema = ` type
我一直在使用 graphql-js 的 Node 服务器中使用 GraphQL,并且 GraphQL 已被证明是一个非常有值(value)的抽象,但我遇到了一个问题。 我经常发现自己需要使用 Grap
对于任何使用过 VuexFire Vuex v2 的人。 mutations: VuexFire.mutations 这里指的是什么?答案可能很明显,但我不知道这意味着什么。那么我的实际突变去哪里了?
我是一名优秀的程序员,十分优秀!