From 33f8c12238fab89f78659ea5b2b8b1340b77f48e Mon Sep 17 00:00:00 2001 From: Leon Xu <6479451-leonxu260@users.noreply.gitlab.com> Date: Sun, 17 Oct 2021 11:46:37 -0700 Subject: [PATCH] doc: create synchronous and asynchronous section --- source/_posts/异步JavaScript.md | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/source/_posts/异步JavaScript.md b/source/_posts/异步JavaScript.md index c2aeb74..7be9ba3 100644 --- a/source/_posts/异步JavaScript.md +++ b/source/_posts/异步JavaScript.md @@ -20,4 +20,26 @@ update: 2021-10-17 11:41:30 我们将函数作为参数传递给其他函数,以便当我们知道预期发生的事情时,可以“稍后回调”。 回调在事件驱动编程中是一个非常强大的特性。 为什么你认为在JavaScript语言中有能力将回调函数作为参数传递给其他函数,如JS中的addEventListener和setTimeout是有用的? -![asynchronous](/images/asynchronous.png) \ No newline at end of file +![asynchronous](/images/asynchronous.png) + +# 同步与异步 +```javascript +(function () { + ... + function init() { + console.log('page loaded'); + qs('button').addEventListener('click', clickHandler); + showMenu(); + } + + function showMenu() { + id('menu').classList.remove('hidden'); + } + + function clickHandler() { + /* Code Here */ + } +}) (); +``` +这基本上是同步的,但有点介于两者之间 +clickHandler不能保证被调用,所以它被推入异步 \ No newline at end of file