本文介绍了在javascript中模拟滚动文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图想出一些东西来模拟一个旧的学校调制解调器连接。 I.E.以可变速度绘制自己而不是一次渲染的文本。

I'm trying to come up with something to simulate an old school modem connection. I.E. text that paints itself at variable speeds rather than rendering at once.

这是我提出的有用的东西:

Here's something that I've come up with that works:

我遇到的问题是我不能让它更快。我的setInterval设置为1ms,这似乎和我一样快。

The problem I have is that I can't get it to go any faster. I've got my setInterval set to 1ms, which seems to be as fast as I can get.

有没有人知道如何重新考虑这个以消除我遇到的速度上限?

Does anybody have any idea how to re-factor this to remove the speed cap I'm experiencing?

推荐答案

当涉及到毫秒速度时,不要使用jQuery,并且至少要缓存引用。要使动画比更快,您一次只能附加多个字符。特别是如果你想要一个类似的速度交叉设备,你应该使用更长的超时,因为 - 标准动画速度是每秒60帧。

Don't use jQuery when it comes to milliseconds-speed, and at least cache the references. To make the animation faster than the minimum timeout, you can only append more than one character a time. Especially if you want a similiar speed cross devices, you should use a longer timeout because the minimum value can vary - a standard animation speed is 60 frames per second.

function displayText(id, text) {
    var node = document.createTextNode(""),
        i = 0,
        chars = 5;
    document.getElementById(id).appendChild(node);

    (function add(){
        node.data += text.substr(i, chars);
        i += chars;
        if (i < text.length)
            setTimeout(add, 1000/60);
    })();
}

这篇关于在javascript中模拟滚动文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:38