refine sound example

This commit is contained in:
Gabe
2016-11-06 02:30:01 -08:00
parent 993f459880
commit ea3f044816
3 changed files with 33 additions and 10 deletions

View File

@@ -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)

View File

@@ -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
View 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);