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

简介: 异步挑战一 sayHowdy问题:思考时间(现在暂时不需要编写代码):分析下方挑战一的代码,打印出来的结果会是怎样顺序的?Howdy先还是Partnah先?题解:123456789101112/* CHALLENGE 1 */function sayHowdy() { console.log('Howdy');}function testMe() { setTimeout(sayHowdy, 0); console.log('Partnah');}// After thinking it through, uncomment t

异步

挑战一 sayHowdy

问题:

思考时间(现在暂时不需要编写代码):分析下方挑战一的代码,打印出来的结果会是怎样顺序的?Howdy先还是Partnah先?

题解:

1
2
3
4
5
6
7
8
9
10
11
12
/* CHALLENGE 1 */
functionsayHowdy() {
console.log('Howdy');
}
functiontestMe() {
  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”。

题解:

1
2
3
4
5
6
7
8
/* CHALLENGE 2 */
functiondelayedGreet() {
// 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”。

代码:

1
2
3
4
5
6
7
8
9
/* CHALLENGE 3 */
functionhelloGoodbye() {
// 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“按钮结束打印如果你对代码的运行满意的话。(译注:原题库网页上的按钮)

题解:

1
2
3
4
5
6
7
8
/* CHALLENGE 4 */
functionbrokenRecord() {
// 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

问题:

构建limitedRepeat函数。其会每秒钟打印一次”hi for now”,但仅仅持续5秒钟。如果你感到困难的话,研究clearInterval。

题解:

1
2
3
4
5
6
7
8
9
/* CHALLENGE 5 */
functionlimitedRepeat() {
// 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

问题:

构建everyXsecsForYsecs函数。其接受三个参数:一个函数func、一个数字interval和另外一个数duration。

everyXsecsForYsecs函数会以interval秒的间隔运行函数func,但会在duration秒后结束运行。

题解:

1
2
3
4
5
6
7
8
9
10
11
12
13
/* CHALLENGE 6 */
functioneveryXsecsForYsecs(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!
functiontheEnd() {
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

问题:

构建delayCounter函数,接受的第一个参数为一个数组(称为target),第二个参数为毫秒单位的数字(称为wait),返回结果为一个函数。

当返回函数被调用时,它会依序打印从1到target之间的数字(含target),以wait毫秒的时间间隔。

题解:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/* CHALLENGE 7 */
functiondelayCounter(target, wait) {
// Solution 1:
let intervalId;
let counter = 0;
returnfunctioninner() {
if (counter === 0) {
        counter++;
        intervalId = setInterval(() =>console.log(inner()), wait);
        } elseif (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

问题:

构建promised函数,接受一个值作为参数。它会返回一个在两秒后触发resolve函数的Promise对象。

提示:到MDN去查阅下Promise对象的文档。

题解:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* CHALLENGE 8 */
functionpromised (val) {
// ADD CODE HERE
const promiseObj = newPromise((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

问题:

编写一个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。

题解:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* CHALLENGE 9 */
classSecondClock{
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

问题:

构建debounce函数,接受参数为一个回调函数callback和一个数值interval,返回结果为一个函数。此返回函数仅会在其上次调用回调函数的interval毫秒后才会被再次调用回调函数。

在interval毫秒时间内调用返回函数不会被响应或列入队列,然而时间信息会被重置( 译注:interval时间重新开始计算)。

有关防抖函数的例子:请查看这个链接 https://css-tricks.com/debouncing-throttling-explained-examples/

题解:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* CHALLENGE 10 */
functiondebounce(callback, interval) {
// ADD CODE HERE
// Solution 1:
let timeCounter = 0
let timeoutId = null
returnfunction() {
if (timeCounter === 0) {
      timeoutId = setTimeout(()=>{timeoutId = null;timeCounter = 0;}, interval);
      timeCounter++;
return callback();
    } else {
if (timeoutId) {
        clearTimeout(timeoutId);
      	timeoutId = setTimeout(()=>{timeoutId = null; timeCounter = 0;}, 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!
functiongiveHi() { return'hi'; }
const giveHiSometimes = debounce(giveHi, 3000);
console.log(giveHiSometimes()); // -> 'hi'
setTimeout(function() { console.log(giveHiSometimes()); }, 2000); // -> undefined
setTimeout(function() { console.log(giveHiSometimes()); }, 4000); // -> undefined
setTimeout(function() { console.log(giveHiSometimes()); }, 8000); // -> 'hi'
相关文章
|
8天前
|
前端开发 JavaScript 开发者
JS 异步解决方案的发展历程以及优缺点
本文介绍了JS异步解决方案的发展历程,从回调函数到Promise,再到Async/Await,每种方案的优缺点及应用场景,帮助开发者更好地理解和选择合适的异步处理方式。
|
9天前
|
JSON 算法 数据可视化
测试专项笔记(一): 通过算法能力接口返回的检测结果完成相关指标的计算(目标检测)
这篇文章是关于如何通过算法接口返回的目标检测结果来计算性能指标的笔记。它涵盖了任务描述、指标分析(包括TP、FP、FN、TN、精准率和召回率),接口处理,数据集处理,以及如何使用实用工具进行文件操作和数据可视化。文章还提供了一些Python代码示例,用于处理图像文件、转换数据格式以及计算目标检测的性能指标。
17 0
测试专项笔记(一): 通过算法能力接口返回的检测结果完成相关指标的计算(目标检测)
|
2月前
|
JavaScript 算法 前端开发
JS算法必备之String常用操作方法
这篇文章详细介绍了JavaScript中字符串的基本操作,包括创建字符串、访问特定字符、字符串的拼接、位置查找、大小写转换、模式匹配、以及字符串的迭代和格式化等方法。
JS算法必备之String常用操作方法
|
2月前
|
JavaScript 算法 前端开发
JS算法必备之Array常用操作方法
这篇文章详细介绍了JavaScript中数组的创建、检测、转换、排序、操作方法以及迭代方法等,提供了数组操作的全面指南。
JS算法必备之Array常用操作方法
|
15天前
|
移动开发 JavaScript 前端开发
【JavaScript】JS执行机制--同步与异步
【JavaScript】JS执行机制--同步与异步
16 0
|
1月前
|
JavaScript 前端开发
一个js里可以有多少个async function,如何用最少的async function实现多个异步操作
在 JavaScript 中,可以通过多种方法实现多个异步操作并减少 `async` 函数的数量。
|
1月前
|
JSON 前端开发 JavaScript
一文看懂 JavaScript 异步相关知识
一文看懂 JavaScript 异步相关知识
27 4
|
2月前
|
JavaScript 算法 前端开发
"揭秘Vue.js的高效渲染秘诀:深度解析Diff算法如何让前端开发快人一步"
【8月更文挑战第20天】Vue.js是一款备受欢迎的前端框架,以其声明式的响应式数据绑定和组件化开发著称。在Vue中,Diff算法是核心之一,它高效计算虚拟DOM更新时所需的最小实际DOM变更,确保界面快速准确更新。算法通过比较新旧虚拟DOM树的同层级节点,递归检查子节点,并利用`key`属性优化列表更新。虽然存在局限性,如难以处理跨层级节点移动,但Diff算法仍是Vue高效更新机制的关键,帮助开发者构建高性能Web应用。
62 1
|
2月前
|
存储 JavaScript API
Node.js中的异步API
【8月更文挑战第16天】
33 1
|
3月前
|
数据采集 JavaScript Python
【JS逆向课件:第十三课:异步爬虫】
回调函数就是回头调用的函数