之后调用sleep函数时,io.write()不会写入控制台 [英] io.write() isn't writing to the console when a sleep function is called afterwards

查看:46
本文介绍了之后调用sleep函数时,io.write()不会写入控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Lua中创建一个简单的迭代输出系统.该代码非常容易理解.

I'm attempting to create a simple iterative output system in Lua. The code is pretty easy to understand.

function IterativeReader.read(Text)
    local Length = string.len(Text)
    
    for Pos = 1,Length do
        io.write(string.sub(Text,Pos,Pos))
        Sleep(ReaderSpeed)
    end

    for i = 1,NewlineCount do
        io.write("\n")
    end
end

您知道,我需要使用 io.write(),因为 print()函数会自动创建换行符,这不是您期望的行为.但是,添加 Sleep 函数(见下文)时会出现此问题.使用繁忙的等待功能会导致相同的问题.

You see, I need to use io.write() as the print() function automatically creates a newline, which is not desired behavior. However, the issue arises when the Sleep function (see below) is added. Using a busy wait function causes the same issue.

function Sleep(t)
  os.execute("sleep "..tonumber(t))
end

经过整个时间后,将输出整个字符串.此问题特定于 io.write(),因为 print()可以很好地工作.

The entire string is output after the full time has passed. This issue is specific to io.write(), as print() works just fine with it.

推荐答案

当您只执行 io.write(...) 时,您的输出会被缓冲.在睡觉前立即执行 io.flush(),以强制随后实际写入输出.

When you just do io.write(...), your output is buffered. Do io.flush() right before sleeping to force the output to be actually written then.

这篇关于之后调用sleep函数时,io.write()不会写入控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆