如何降低执行函数的速度
How do I decrease the speed of executing a function?
我知道我可以使用setTimeout()
来做到这一点,但我的情况有点危急。下面是一个例子:
var nameinput = document.getElementById("typename");
var printed = document.getElementById("name");
function demo(){
setTimeout(function(){printed.innerHTML = nameinput.value.toLowerCase()}, 800);
}
nameinput.addEventListener("input", demo);
#name {
height: 50px;
width: 400px;
margin: 20px auto;
border: 1px solid lightgreen;
border-radius: 4px;
font-family: monospace;
font-size: 200%;
font-weight: 100;
line-height: 50px;
}
#typename {
font-size: 150%;
line-height: 40px;
height: 40px;
width: 30%;
min-width: 350px;
border: 1px solid skyblue;
display: block;
margin: 20px auto;
padding: 5px;
border-radius: 4px;
}
<div id="name"></div>
<p>some text</p>
<input id="typename" type="text"/>
我上面提供的代码将我的文本作为输入,并在指定的延迟之后打印它,但是只有当我等到每个字母输入之后的那个时间才工作。
总之,我希望它像这样工作:"我可以在0.8秒内输入一个单词,但它会在每个字母的0.8秒后输入每个字母"
如果您需要捕获数组中的每个更改,请尝试以下操作:
var nameinput = document.getElementById("typename");
var printed = document.getElementById("name");
var actions = []
function demo() {
var old = actions.length ?
actions[actions.length - 1].old : printed.innerHTML
var str = nameinput.value.toLowerCase()
if (old.length < str.length)
actions.push({
f: add.bind(null, str.slice(-1)),
old: str
})
else if (old.length > str.length)
actions.push({
f: del,
old: str
})
}
function play() {
if (!actions.length)
return
var a = actions.shift()
a.f()
}
function add(s) {
printed.innerHTML += s
}
function del() {
printed.innerHTML = printed.innerHTML.slice(0, printed.innerHTML.length - 1)
}
setInterval(play, 800)
nameinput.addEventListener("input", demo);
#name {
height: 50px;
width: 400px;
margin: 20px auto;
border: 1px solid lightgreen;
border-radius: 4px;
font-family: monospace;
font-size: 200%;
font-weight: 100;
line-height: 50px;
}
#typename {
font-size: 150%;
line-height: 40px;
height: 40px;
width: 30%;
min-width: 350px;
border: 1px solid skyblue;
display: block;
margin: 20px auto;
padding: 5px;
border-radius: 4px;
}
<div id="name"></div>
<p>some text</p>
<input id="typename" type="text" />
这只是一个测试,似乎工作,但可能有一些缺陷的地方
这是我的看法。如果当前输出不是输入值的子字符串,则清除现有文本。
将所有内容包装在单个变量中,以避免与任何现有脚本的名称冲突,并添加了一些控件,以便更容易实现。
var iO = {
interval: 800,
inputSelector: '#input',
outputSelector: '#output',
updating: false,
start: function () {
iO.updating ||
( iO.updating = setInterval(iO.update, iO.interval),
iO.update()
)
},
stop: function () {
clearInterval(iO.updating);
iO.updating = false
},
update: function () {
var i = document.querySelector(iO.inputSelector),
o = document.querySelector(iO.outputSelector),
$in = i.value,
$out = o.innerHTML;
if ($out == $in) {
// update finished. stop.
iO.stop();
return
}
if (!$in.length) {
// no input. clear output. stop.
o.innerHTML = '';
iO.stop();
return
}
if ($out.length > $in.length || $in.indexOf($out) == -1) {
// output > input or output not found in input. clear output.
o.innerHTML = '';
}
if (typeof $in[$out.length] !== "undefined") {
// next letter is defined. add it to output.
o.innerHTML += $in[$out.length];
}
if (o.innerHTML == $in) {
// update finished. stop.
iO.stop();
}
}
};
document.getElementById("input").addEventListener("keyup", iO.start);
body {
background-color: #f5f5f5;
margin: 0;
padding: 0;
}
#input, #output {
font-size: 2.1rem;
line-height: 2.1rem;
min-height: 4rem;
min-width: calc(100vw - 6rem);
max-width: 80vw;
margin: 2rem auto 1rem;
display: block;
line-height: 2rem;
padding: .5rem 1rem;
box-sizing: border-box;
}
#output {
background-color: white;
border-radius: 2px;
box-shadow: 0 1px 5px 0 rgba(0,0,0,.1),0 2px 2px 0 rgba(0,0,0,.07),0 3px 1px -2px rgba(0,0,0,.06);
min-height: 10rem;
padding: 1rem;
}
<input id="input" type="text" />
<div id="output"></div>
在用户输入时创建一个闭包:
function demo(){
setTimeout(
function(inp){
return function(){
printed.innerHTML = inp;
}
}(nameinput.value.toLowerCase()), 800);
}
存储一个在设置超时时输入的要执行的闭包。
相关文章:
- 在固定间隔内调用 AJAX 函数是否会减慢应用程序的速度
- 鼠标速度 JavaScript 函数
- jQuery 事件回调速度:匿名与命名函数
- 在函数中做未使用的参数会减慢 JavaScript 的执行速度
- 我可以减慢这个Javascript函数的速度吗?
- JS计数器:在一个函数中设置不同的速度(间隔)
- 将对象传递到同一作用域内的函数的速度较慢
- jQuery:当重复调用时,重绘函数的速度非常慢
- 为什么加载 JavaScript 函数的工作速度比预期的慢
- Javascript函数可以降低滚动速度
- 如何比较两个Javascript函数的速度
- 包含函数的对象文字表示法的执行速度是否比(全局范围)普通函数(取消引用)更快
- 如何使setTimout函数以相同的速度运行
- 循环中的异步函数运行速度太慢.我该如何提高表现
- 当我使用setInterval函数时,画布中的对象会以指数级的速度增长
- 一个函数JQuery的速度不同
- 我将所有内容保存在外部.js文件中.但并不是所有的函数都在每个页面上使用.这会影响速度吗?
- Javascript中记忆fibonacci函数的奇怪速度结果
- 如何减慢jquery单击函数的执行速度?
- 如何降低执行函数的速度