在 JavaScript 中顺序触发多个随机超时

发布于 2024-07-13 03:44:32 字数 982 浏览 6 评论 0原文

我知道乍一看(由于标题)这看起来像是“你在发帖之前尝试过搜索谷歌吗?”之一。 问题,但我似乎无法找到我遇到的具体问题的答案。 抱歉,如果我是菜鸟……仍在学习:)

我需要在 javascript 中模拟暂停,但 setTimeout(function_call, timeout) 函数对我不起作用。 原因是...当调用 setTimeout 函数时,它会异步调用该函数。

一点背景:
我正在尝试模拟以随机时间间隔将文本输入到 div 中。 我希望它看起来就像一个人在查看页面时实际上正在输入消息一样。 由于超时是随机间隔并且函数调用是异步进行的,因此文本最终会以随机顺序打印。

这是我迄今为止所掌握的内容的片段:

typeString: function(s)
{
    for(var i=0;i<s.length;i++)
    {                      
        var c = s.charAt(i);
        var temp = setTimeout("MessageType.typeChar('" + c + "')", this.genRandomTime());
    }
}



在此先感谢您的帮助。

CJAM



更新:通过将计时器延迟添加到变量中,它使我能够抵消异步调用的超时。 感谢大家的快速回复。 这是更新后的代码:

typeString: function(s)
{
    var delay = 0;

    for(var i=0;i<s.length;i++)
    {                     
        var c = s.charAt(i);
        setTimeout("GoogleTyper.typeChar('"+c+"')", delay += this.genRandomTime());
    }
}

I know at first glance (due to the title) this looks like one of the "Did you try searching Google before posting?" questions, but I can't seem to find an answer for the specific issue I'm experiencing. Sorry if I'm a noob.... still learning :)

I need to simulate a pause in javascript, but the setTimeout(function_call, timeout) function is not working for me. Reason being... when that setTimeout function is called, it then makes the function call asynchronously.

A little background:
I'm trying to simulate text being typed into a div at randomly timed intervals. I want it to appear as if a person is actually typing a message while the user is viewing the page. Since the timeout is a random interval and the function call is made asynchronously, the text ends up being printed in a random order.

Here is a snippet of what I have thus far:

typeString: function(s)
{
    for(var i=0;i<s.length;i++)
    {                      
        var c = s.charAt(i);
        var temp = setTimeout("MessageType.typeChar('" + c + "')", this.genRandomTime());
    }
}

Thanks in advance for your help.

CJAM

UPDATE: By adding the timer delay to a varialbe, it enabled me to offset timeOut for the asynchronous calls. Thank you all for your quick responses. Here is the updated code:

typeString: function(s)
{
    var delay = 0;

    for(var i=0;i<s.length;i++)
    {                     
        var c = s.charAt(i);
        setTimeout("GoogleTyper.typeChar('"+c+"')", delay += this.genRandomTime());
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅笑轻吟梦一曲 2024-07-20 03:44:32

您是否尝试过累积设置超时? 将一个变量放在循环之外并将其初始化为 0。现在我们将其称为超时。 在循环的每次迭代开始时向此变量添加随机时间量,并使超时发挥作用。 这样,您就可以确保按顺序调用函数。

Have you tried cumulatively setting the timeout? Stick a variable outside of your loop and initialize it to 0. Let's call it timeout for now. Add a random amount of time to this variable at the beginning of each iteration of your loop, and make the timeout work off of that. This way, you are ensured that functions are being called in order.

断念 2024-07-20 03:44:32

您的问题是您正在使用一个计时器,从现在开始您的所有时间都被延迟 - 下一个计时器需要在前一个计时器之后触发。 只需将之前的计时器延迟添加到新计时器即可。

typeString: function(s)
{
    var delay = 0;
    for(var i=0;i<s.length;i++)
    {                      
        var c = s.charAt(i);
        delay = delay + this.genRandomTime();
        var temp = setTimeout("MessageType.typeChar('" + c + "')", delay );
    }
}

Your problem is you are using one all your times are delayed starting from now - the next timer needs to be fired after the previous. Simply add the previous timer delay to the new timer.

typeString: function(s)
{
    var delay = 0;
    for(var i=0;i<s.length;i++)
    {                      
        var c = s.charAt(i);
        delay = delay + this.genRandomTime();
        var temp = setTimeout("MessageType.typeChar('" + c + "')", delay );
    }
}
这样的小城市 2024-07-20 03:44:32

不要将要显示的字符传递给每个计时器事件,而是让每个事件处理程序从队列(数组、列表等)中获取下一个字符,以便保持顺序。

Instead of passing to each timer event that character to be displayed, let each event handler grab the next character off of a queue (array, list whatever), so the order is maintained.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文