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

推荐订阅源

博客园_首页
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Know Your Adversary
Know Your Adversary
Latest news
Latest news
H
Help Net Security
C
CERT Recently Published Vulnerability Notes
阮一峰的网络日志
阮一峰的网络日志
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Cyber Attacks, Cyber Crime and Cyber Security
Security Latest
Security Latest
B
Blog RSS Feed
V
Vulnerabilities – Threatpost
T
Threatpost
量子位
P
Proofpoint News Feed
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Cisco Talos Blog
Cisco Talos Blog
F
Fortinet All Blogs
Cyberwarzone
Cyberwarzone
Recorded Future
Recorded Future
博客园 - 聂微东
博客园 - 叶小钗
云风的 BLOG
云风的 BLOG
Forbes - Security
Forbes - Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
小众软件
小众软件
N
Netflix TechBlog - Medium
aimingoo的专栏
aimingoo的专栏
酷 壳 – CoolShell
酷 壳 – CoolShell
I
InfoQ
Last Week in AI
Last Week in AI
N
News and Events Feed by Topic
Google DeepMind News
Google DeepMind News
宝玉的分享
宝玉的分享
Hugging Face - Blog
Hugging Face - Blog
PCI Perspectives
PCI Perspectives
T
Tenable Blog
C
Cybersecurity and Infrastructure Security Agency CISA
L
LangChain Blog
SecWiki News
SecWiki News
H
Hacker News: Front Page
WordPress大学
WordPress大学
www.infosecurity-magazine.com
www.infosecurity-magazine.com
S
Schneier on Security
H
Heimdal Security Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园 - 【当耐特】
A
About on SuperTechFans
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tailwind CSS Blog

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