wait
同期XMLHttpRequestを使ってJavaScriptでwaitする。
wait.html
<!DOCTYPE html> <html> <head> <title>wait test</title> <script>window.onload = function () { var script = document.getElementsByTagName("script").item(0); var code = document.getElementsByTagName("code").item(0); code.appendChild(document.createTextNode(script.textContent)); function wait(sec) { var API_URL = "wait.rb"; var req = new XMLHttpRequest(); req.open("GET", API_URL + "?wait=" + encodeURIComponent(sec), false); req.send(null); } var i; for (i = 1; i <= 3; i++) { alert(i); if (i < 3) wait(1); } };</script> </head> <body> <pre><code></code></pre> </body> </html>
wait.rb
#!/usr/bin/env ruby require 'webrick/cgi' class WaitCGI < WEBrick::CGI def do_GET(req, res) sleep(req.query["wait"].to_i) res.body = req.query_string end end WaitCGI.new.start