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

推荐订阅源

Spread Privacy
Spread Privacy
大猫的无限游戏
大猫的无限游戏
F
Fortinet All Blogs
M
MIT News - Artificial intelligence
G
Google Developers Blog
Hacker News: Ask HN
Hacker News: Ask HN
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
H
Help Net Security
V
Visual Studio Blog
WordPress大学
WordPress大学
博客园 - 司徒正美
TaoSecurity Blog
TaoSecurity Blog
Webroot Blog
Webroot Blog
Hugging Face - Blog
Hugging Face - Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
W
WeLiveSecurity
C
CERT Recently Published Vulnerability Notes
Y
Y Combinator Blog
S
Schneier on Security
Recent Announcements
Recent Announcements
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
宝玉的分享
宝玉的分享
T
Troy Hunt's Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 三生石上(FineUI控件)
Microsoft Azure Blog
Microsoft Azure Blog
N
News and Events Feed by Topic
A
About on SuperTechFans
小众软件
小众软件
K
Kaspersky official blog
Help Net Security
Help Net Security
V2EX - 技术
V2EX - 技术
P
Proofpoint News Feed
S
Secure Thoughts
IT之家
IT之家
云风的 BLOG
云风的 BLOG
N
Netflix TechBlog - Medium
The Register - Security
The Register - Security
I
Intezer
NISL@THU
NISL@THU
GbyAI
GbyAI
P
Privacy & Cybersecurity Law Blog
T
Threatpost
C
Check Point Blog
Forbes - Security
Forbes - Security
C
Cisco Blogs
AI
AI
Recorded Future
Recorded Future
F
Full Disclosure

博客园 - 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);