0

我有一个 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)

任何帮助表示赞赏!

4

1 回答 1

3

Ruby on rails 呈现从服务器发送到浏览器的 HTML。所以你使用的 sleep 方法只会延迟服务器完成页面渲染。

如果您想在浏览器中使用此功能,则必须使用 javascript。

于 2013-09-13T12:20:47.970 回答