我有一个 Ruby 脚本,它在控制台上打印字符时会插入一点延迟。我想在网页上做同样的事情,但是当我从演示站点调用脚本时,所有内容都会立即打印出来。这是脚本:
def add_breath(text, pause)
# print pause
char_list = text.scan(/./)
print "\n"
# now iterate through the char list and print each char with a slight delay
for char in char_list
if char == " " #or char == "?" or char == "!" or punctuation
sleep(0.02)
print char
elsif char == "," or char == "." or char == "!" or char == ":"
sleep(0.22)
print char
elsif char == '@'
sleep(pause)
elsif char == char.downcase or char == char.upcase
sleep(0.05)
print char
end
end
sleep(0.5)
print "\n"
#writes user input to file
target = File.open("sleep.txt", 'w')
target.write(text)
target.write("\n")
target.close()
end
text = "Make your sentences breath like this -- @O@M@G@!@ -- with the 'at' character (on US keyboards, that's shift+2). Your turn:"
# text = "Well, give it a try:"
add_breath(text, 0.3)
text = gets
puts "OK, now tell me how long each breath should be (try 0.3):"
pause = gets.to_f
add_breath(text, pause)
任何帮助表示赞赏!