JavaScript专项算法题(4):异步

异步

挑战一 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值