当前位置:首页 > 前端 > 正文内容

【原创】Promise循环执行,以及延时循环

Z先生5年前 (2020-11-28)前端995

演示代码

//核心代码
function promiseMain(page) {
	return new Promise(function(resolve, reject) {
		if (page <= 5) {
			resolve(new Date().getTime());
		} else {
			resolve(false);
		}
	})
}

//前置代码
function promiseForeach(page) {
	promiseMain(page).then(function(res) {
		if (res) {
			console.log('time', res);
			page++;
			setTimeout(function() {
				promiseForeach(page);
			},
			3000);
		}
	}).
	catch(function(e) {
		console.log(e);
	})
}
//调用执行
promiseForeach(1);


执行结果如下:

> "time" 1606548851338
> "time" 1606548854340
> "time" 1606548857926
> "time" 1606548861913
> "time" 1606548865924


标签: vuejsPromise
分享给朋友:

相关文章

VUE/UNIAPP事件传参

第一种方式使用data<view @click="handleClick" data-info="大家好">点击按钮</v...

antd vue版一些小细节

复选框默认选择复选框选择默认选择,但是不要使用属性手上的checked,否则渲染后无法动态修改,有defaultChecked方法,但是测试无效,所以使用valuePropName: <pre...

JS数组查找、删除元素

数组查找方法一:indexOf()——ES5const array = ['apple', 'banance', 'orange']array.indexOf('apple')...

vue深度监听

还是不废话,直接上干货 watch: { data: { handler: function(newVal, oldVal) { cons...

问题: - did you register the component correctly......

问题: - did you register the component correctly......

问题:无法使用tree组件提示 Unknown custom element:<a-tree> - did you register the component correctly? Fo...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。