mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-01-13 16:57:54 -05:00
refine sound example
This commit is contained in:
@@ -2,13 +2,13 @@ sleep = (ms) ->
|
||||
new Promise (resolve) ->
|
||||
window.setTimeout resolve, ms
|
||||
|
||||
window.countdown = (seconds) ->
|
||||
if not window.speechSynthesis?
|
||||
alert('speech API not supported in your browser')
|
||||
return
|
||||
|
||||
countdown = (seconds) ->
|
||||
for i in [seconds..1]
|
||||
utterance = new SpeechSynthesisUtterance("#{i}")
|
||||
speechSynthesis.speak(utterance)
|
||||
await sleep(1000) # wait one second
|
||||
alert "done!"
|
||||
if window.speechSynthesis?
|
||||
utterance = new SpeechSynthesisUtterance "#{i}"
|
||||
window.speechSynthesis.speak utterance
|
||||
console.log i
|
||||
await sleep 1000 # wait one second
|
||||
alert "Done! (Check the console!)"
|
||||
|
||||
countdown(3)
|
||||
|
||||
@@ -868,7 +868,7 @@ Block
|
||||
may be used if you need to force a generator that doesn't yield.
|
||||
</p>
|
||||
|
||||
<%= codeFor('async', 'countdown(3)') %>
|
||||
<%= codeFor('async', true) %>
|
||||
<p>
|
||||
Similar to how <code>yield return</code> forces a generator, <code>await return</code>
|
||||
may be used to force a function to be async.
|
||||
|
||||
23
documentation/js/async.js
Normal file
23
documentation/js/async.js
Normal file
@@ -0,0 +1,23 @@
|
||||
// Generated by CoffeeScript 2.0.0-alpha
|
||||
var countdown, sleep;
|
||||
|
||||
sleep = function(ms) {
|
||||
return new Promise(function(resolve) {
|
||||
return window.setTimeout(resolve, ms);
|
||||
});
|
||||
};
|
||||
|
||||
countdown = async function(seconds) {
|
||||
var i, j, ref, utterance;
|
||||
for (i = j = ref = seconds; ref <= 1 ? j <= 1 : j >= 1; i = ref <= 1 ? ++j : --j) {
|
||||
if (window.speechSynthesis != null) {
|
||||
utterance = new SpeechSynthesisUtterance("" + i);
|
||||
window.speechSynthesis.speak(utterance);
|
||||
}
|
||||
console.log(i);
|
||||
await sleep(1000);
|
||||
}
|
||||
return alert("Done! (Check the console!)");
|
||||
};
|
||||
|
||||
countdown(3);
|
||||
Reference in New Issue
Block a user