惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

O
OpenAI News
I
Intezer
C
CERT Recently Published Vulnerability Notes
V
Vulnerabilities – Threatpost
S
Securelist
C
Cyber Attacks, Cyber Crime and Cyber Security
G
GRAHAM CLULEY
P
Palo Alto Networks Blog
P
Privacy & Cybersecurity Law Blog
T
Tenable Blog
T
Threatpost
Latest news
Latest news
Cisco Talos Blog
Cisco Talos Blog
A
Arctic Wolf
罗磊的独立博客
云风的 BLOG
云风的 BLOG
L
LangChain Blog
博客园 - 【当耐特】
P
Privacy International News Feed
The GitHub Blog
The GitHub Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
www.infosecurity-magazine.com
www.infosecurity-magazine.com
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Troy Hunt's Blog
量子位
Security Archives - TechRepublic
Security Archives - TechRepublic
爱范儿
爱范儿
S
Security @ Cisco Blogs
Martin Fowler
Martin Fowler
博客园_首页
SecWiki News
SecWiki News
Spread Privacy
Spread Privacy
I
InfoQ
博客园 - 司徒正美
T
Tor Project blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Help Net Security
L
LINUX DO - 最新话题
H
Heimdal Security Blog
TaoSecurity Blog
TaoSecurity Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Vercel News
Vercel News
Y
Y Combinator Blog
D
DataBreaches.Net
T
Threat Research - Cisco Blogs
Stack Overflow Blog
Stack Overflow Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
M
MIT News - Artificial intelligence
Recorded Future
Recorded Future
博客园 - 叶小钗

博客园 - jowo

websql操作类封装 async/await 来处理异步详解 使用富文本编辑器wangEditor3 Promise的基本用法 zTree setting 配置 WebSQL vue-cli快速构建项目 接收的第一个参数是模块的局部状态对象。 如何使用JavaScript检测Ctrl+V,Ctrl+C? 什么情况下我应该使用 Vuex? Vue+node.js实现一个简洁的个人博客系统 Vuex 是什么? vue中mixins的理解及应用 Vue:slot用法 web.xml文件的的param-name Robots协议一定放在网站根目录下 浅谈重写与重载 笔记纪要:C# WebService URL重写 Java应用监控利器JMX
Vue.js中Promise、异步、同步、定时器
jowo · 2020-05-27 · via 博客园 - jowo

Posted on 2020-05-27 16:42  jowo  阅读(1144)  评论()    收藏  举报

Vue.js使用Axios或Ajax请求后台时,是异步请求,所有的请求同时执行,后面的then随机执行

要想实现同步执行可以使用await和async

在需要同步执行(即后面一个方法的参数是前一个方法的返回值),在父层方法前面加async,并且在字层方法前面加await,

然后方法中需返回Promise对象,用resolve()表示执行成功,

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

async mounted() {

    this.currentTime = parseTime(

      new Date().toString(),

      '{y}-{m}-{d} {h}:{i}:{s}'

    )

    await this.getOrgs()

    await this.getQuDao()

    await this.getProductList()

    await this.queryData()

  },

getOrgs() {

  return newPromise((resolve,reject) => {

     ...

     resolve(true)

  }) 

}

getQuDao() {

  return newPromise((resolve,reject) => {

     ...

     resolve(true)

  }) 

}

...

当需要等到所有的请求都成功完成请求数据,再做其他操作时可以用到Promise.All方法

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

initPage() {

  const _amt = this.getData()

  ...

  Promise.all([

        _amt,_amtPie,

        _amtRepay,_repayAmtPie,

        _num,_intoPie,

        _rate,_rateBar,

        _map,_table

      ]).then(([_amt, _amtPie, _amtRepay, _repayAmtPie, _num, _intoPie, _rate,

  _rateBar, _map, _table]) => {

      ...

      })

      .catch(() => {

       ...

      })

}

getData() {

  return new Promise((resolve,reject) => {

    gethttpData({}).then(response => {

       ... 

     }).catch(err => {

       ...

      })

  }) 

}

定时器(1)

setTimeout只在指定时间后执行一次,代码如下:

1

2

3

4

5

6

7

8

9

10

<script> 

function hello(){ 

alert("hello"); 

var t1 = window.setTimeout(hello,1000); 

var t2 = window.setTimeout("hello()",3000);

window.clearTimeout(t1);

</script>  

定时器(2)

setInterval以指定时间为周期循环执行,代码如下:

1

2

3

4

5

6

setInterval('refreshQuery()',8000);  

function refreshQuery(){ 

   $("#mainTable").datagrid('reload',null);