@冒泡的马树
题库原地址:http://csbin.io/async
异步
挑战一
问题:
思考工夫(当初临时不须要编写代码):剖析下方挑战一的代码,打印进去的后果会是怎么程序的?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函数,用于在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函数。其会立即打印”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函数。其会每秒钟都打印一次”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 second): hi again
挑战五
问题:
构建limitedRepeat函数。其会每秒钟打印一次”hi for now",但仅仅继续5秒钟。如果你感到艰难的话,钻研clearInterval。
题解:
/* CHALLENGE 5 */function limitedRepeat() { // ADD CODE HERE const intervalId = setInterval(()=>console.log('hi for now'), 1000); setTimeout(()=>clearInterval(intervalId), 5000);}// Uncomment the following line to check your work!// limitedRepeat(); // should log (every second, for 5 seconds): hi for now
挑战六
问题:
构建everyXsecsForYsecs函数。其承受三个参数:一个函数func、一个数字interval和另外一个数duration。
everyXsecsForYsecs函数会以interval秒的距离运行函数func,但会在duration秒后完结运行。
题解:
/* CHALLENGE 6 */function everyXsecsForYsecs(func, interval, duration) { // ADD CODE HERE const intervalId = setInterval(func, interval * 1000); setTimeout(() => clearInterval(intervalId), duration * 1000); }// Uncomment the following lines to check your work!function theEnd() { console.log('This is the end!');}everyXsecsForYsecs(theEnd, 2, 20); // should invoke theEnd function every 2 seconds, for 20 seconds): This is the end!
挑战七
问题:
构建delayCounter函数,承受的第一个参数为一个数组(称为target),第二个参数为毫秒单位的数字(称为wait),返回后果为一个函数。
当返回函数被调用时,它会以依序打印从1到target之间的数字(含target),以wait毫秒的工夫距离。
题解:
/* CHALLENGE 7 */function delayCounter(target, wait) { // Solution 1: let intervalId; let counter = 0; return function inner() { if (counter === 0) { counter++; intervalId = setInterval(() => console.log(inner()), wait); } else if (counter === target) { clearInterval(intervalId); return counter; } else { return counter++; } } // Solution 2: //return function inner() { // for(let i = 1; i<=target; i++){ // setTimeout(()=>console.log(i), wait * i); // } //}}// UNCOMMENT THESE TO TEST YOUR WORK!// const countLogger = delayCounter(3, 1000)// countLogger();// After 1 second, log 1// After 2 seconds, log 2// After 3 seconds, log 3
挑战八
问题:
构建promised函数,承受一个值作为参数。它会返回一个在两秒后触发resolve函数的Promise对象。
提醒:到MDN去查阅下Promise对象的文档。
题解:
/* CHALLENGE 8 */function promised (val) { // ADD CODE HERE const promiseObj = new Promise((resolve) => { setTimeout(() => resolve(val), 2000); }); return promiseObj;}// UNCOMMENT THESE TO TEST YOUR WORK!// const createPromise = promised('wait for it...');// createPromise.then((val) => console.log(val)); // will log "wait for it..." to the console after 2 seconds
挑战九
问题:
编写一个SecondClock类。其有两个办法:start和reset。
start:当调用时,start会每秒调用一个回调函数(this.cb,在结构器中定义),作用于一个变量。这个变量每次被回调函数应用时总是以后的工夫秒数。
换言之,此回调函数每一秒钟都基于时钟信号的秒数而被调用,总是从1开始但并不应用以后计算机上的时钟信号的秒数值。
第一次“滴答”(值为1)产生在最后的secondClock调用的1秒后;
第二次“滴答”(值为2)产生在最后的secondClock调用的2秒后;
……
第六十次“滴答”(值为60)产生在最后的secondClock调用的60秒后;
第六十一次“滴答”(值为61)产生在最后的secondClock调用的61秒后;
第六十二次“滴答”(值为62)产生在最后的secondClock调用的62秒后;
以此类推。
reset:当调用时,齐全进行SecondClock时钟的运行,另外重设工夫为初始值。
提醒:查阅setInterval和clearInterval。
题解:
/* CHALLENGE 9 */class SecondClock { constructor(cb) { // ADD CODE HERE this.counter = 0 this.intervalId = 0 this.cb = cb } // ADD METHODS HERE start () { this.intervalId = setInterval(()=>this.cb(++this.counter), 1000); } reset () { clearInterval(this.intervalId); this.counter = 0; this.intervalId = 0; }}// UNCOMMENT THESE TO TEST YOUR WORK!// const clock = new SecondClock((val) => { console.log(val) });// console.log("Started Clock.");// clock.start();// setTimeout(() => {// clock.reset();// console.log("Stopped Clock after 6 seconds.");// }, 6000);
挑战十
问题:
构建debounce函数,承受参数为一个回调函数callback和一个数值interval,返回后果为一个函数。此返回函数仅会在其上次调用回调函数的interval毫秒后才会被再次调用回调函数。
在interval毫秒工夫内调用返回函数不会被响应或列入队列,然而工夫信息会被重置( 译注:interval工夫从新开始计算)。
无关防抖函数的例子:请查看这个链接 https://css-tricks.com/deboun...
题解:
/* CHALLENGE 10 */function debounce(callback, interval) { // ADD CODE HERE // Solution 1: let timeCounter = 0 let timeoutId = null return function() { if (timeCounter === 0) { timeoutId = setTimeout(()=>timeoutId = null, interval); timeCounter++; return callback(); } else { if (timeoutId) { clearTimeout(timeoutId); timeoutId = setTimeout(()=>timeoutId = null, interval); timeCounter++; } else { timeCounter++; return callback(); } } } // // Solution 2 (Not efficient): // return function (){// if (timeoutId) {// clearTimeout(timeoutId);// timeoutId = setTimeout(() => timeoutId = null, interval);// } else {// timeoutId = setTimeout(() => timeoutId = null, interval);// return callback(); // }// }}// UNCOMMENT THESE TO TEST YOUR WORK!function giveHi() { return 'hi'; }const giveHiSometimes = debounce(giveHi, 3000);console.log(giveHiSometimes()); // -> 'hi'setTimeout(function() { console.log(giveHiSometimes()); }, 2000); // -> undefinedsetTimeout(function() { console.log(giveHiSometimes()); }, 4000); // -> undefinedsetTimeout(function() { console.log(giveHiSometimes()); }, 8000); // -> 'hi'