异步
挑战一 sayHowdy
问题:
思考时间(现在暂时不需要编写代码):分析下方挑战一的代码,打印出来的结果会是怎样顺序的?Howdy先还是Partnah先?
题解:
/* CHALLENGE 1 */
function sayHowdy() {
console.log('Howdy');
}
function testMe() {
setTimeout(sayHowdy, 0);
console.log('Partnah');
}
// After thinking it through, uncomment the following line to check your guess!
// testMe(); // what order should these log out? Howdy or Partnah first?
挑战二 delayedGreet
问题:
构建delayedGreet函数,用于在3秒后打印“welcome"。
题解:
/* CHALLENGE 2 */
function delayedGreet() {
// ADD CODE HERE
setTimeout(()=>console.log('welcome'), 3000);
}
// Uncomment the following line to check your work!
// delayedGreet(); // should log (after 3 seconds): welcome
挑战三 helloGoodbye
问题:
构建helloGoodbye函数。其会立刻打印”hello",然后2秒后打印“good bye"。
代码:
/* CHALLENGE 3 */
function helloGoodbye() {
// ADD CODE HERE
setTimeout(()=>console.log('good bye'), 2000);
console.log('hello');
}
// Uncomment the following line to check your work!
// helloGoodbye(); // should log: hello // should also log (after 3 seconds): good bye
挑战四 brokenRecord
问题:
构建brokenRecord函数。其会每秒钟都打印一次”hi again“。使用”End Code“按钮结束打印如果你对代码的运行满意的话。(译注:原题库网页上的按钮)
题解:
/* CHALLENGE 4 */
function brokenRecord() {
// ADD CODE HERE
setInterval(()=>console.log('hi again'), 1000);
}
// Uncomment the following line to check your work!
// brokenRecord(); // should log (every se