【发布时间】:2020-03-28 23:37:15
【问题描述】:
我已经构建了一种密码生成器,它应该在倒计时到期时显示一个新密码。不幸的是,我只设法弄清楚如何运行我的代码一次。倒计时由一个简单的 CSS 过渡组成,我想保留它,因为它比我尝试使用 JavaScript 反复更新宽度的其他尝试平滑得多。
var dictionary = {
"adverbs": [
"always",
"usually",
"probably"
],
"adjectives": [
"useful",
"popular",
"accurate"
],
"nouns": [
"computer",
"software",
"terminal"
]
};
function capitalizeFirst(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function randomIndex(object) {
return object[Math.floor(Math.random() * object.length)];
}
function generatePassword() {
var category = ["adverbs", "adjectives", "nouns"];
var password = [];
for (i = 0; i < category.length; i++) {
password.push(capitalizeFirst(randomIndex(dictionary[category[i]])));
}
password.push(Math.floor(Math.random() * 8999) + 1000);
return password.join("");
}
function updatePassword() {
document.getElementById("countdown-fill").style.width = 100 + '%';
document.getElementById("text-field").value = generatePassword();
document.getElementById("countdown-fill").style.width = 0 + '%';
}
setInterval(updatePassword, 5000);
@import url('https://fonts.googleapis.com/css?family=Nunito&display=swap');
body {
margin: 0;
width: 100%;
height: 100%;
background-color: #f8f8f8;
}
.container {
max-width: 400px;
margin: 0 auto;
}
#text-field {
font-size: 15px;
font-weight: 400;
font-family: 'Nunito', sans-serif;
margin-top: 100px;
margin-bottom: 10px;
width: 100%;
height: 30px;
padding: 10px;
box-sizing: border-box;
border: 1px solid #e5e5e5;
background-color: #ffffff;
}
#countdown-background {
width: 100%;
height: 10px;
box-sizing: border-box;
border: 1px solid #e5e5e5;
background-color: #ffffff;
}
#countdown-fill {
width: 100%;
height: 100%;
transition: width 5s;
transition-timing-function: linear;
background-color: #1e87f0;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Password Generator</title>
</head>
<body>
<div class="container">
<input id="text-field" type="text" spellcheck="false">
<div id="countdown-background">
<div id="countdown-fill"></div>
</div>
</div>
</body>
</html>
目前,我的代码有两个明显的问题:
- 由于
setInterval,转换变得延迟。如果我只是单独调用updatePassword,情况并非如此。 - CSS 过渡只动画一次。我想在每次调用
updatePassword时重置动画。
我遇到了一些 jQuery 解决方案来解决我的问题,但我对这些解决方案不是很感兴趣,因为我想尽可能地依赖标准 JavaScript。不过,我可以使用关键帧等替代 CSS 工具,它们似乎运行良好:
#countdown-fill {
width: 100%;
height: 100%;
animation: refresh 5s infinite;
background-color: #1e87f0;
}
@keyframes refresh {
from {
width: 100%;
}
to {
width: 0;
}
}
不过,我确实担心同步问题,因为动画与 updatePassword 没有任何关联。
问题:有没有办法让updatePassword在我每次调用该函数时重置动画,并消除初始延迟?
【问题讨论】:
标签: javascript css