如何用原生js完成一个时间上随机并且固定执行一系列相同任务的功能?

1.完成如下功能

每隔5分钟到10分钟中间的一个时间点(时间随机),即每次间隔的时间都不一样,执行几个动作的循环,动作与动作之间可以任意设置等待时间

执行的动作不会变,也就是大概这样

//执行动作一
function a(){

}

//等待十秒
//执行动作二
function b(){

}

//等待二十秒
//执行动作三
function b(){

}


莫回无
浏览 507回答 1
1回答

HUWWW

(function go(){&nbsp; &nbsp; setTimeout(function(){&nbsp; &nbsp; &nbsp; &nbsp; setTimeout(function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; child1();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTimeout(function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; child2();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTimeout(function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; child3();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; go()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },20秒)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },30秒),&nbsp; &nbsp; &nbsp; &nbsp; },10秒),&nbsp; &nbsp; },5分钟)})()应该用 promise 来改写。。。。从新改写了一下,如下:var list = [{ // 按顺序循环执行的函数列表&nbsp; &nbsp; fn: function() {&nbsp; &nbsp; &nbsp; &nbsp; console.log(1)&nbsp; &nbsp; },&nbsp; &nbsp; time: 1000}, {&nbsp; &nbsp; fn: function() {&nbsp; &nbsp; &nbsp; &nbsp; console.log(2)&nbsp; &nbsp; },&nbsp; &nbsp; time: 1000}, {&nbsp; &nbsp; fn: function() {&nbsp; &nbsp; &nbsp; &nbsp; console.log(3)&nbsp; &nbsp; },&nbsp; &nbsp; time: 1000}];(function start(list) {&nbsp; &nbsp; var wrapList = list.map(function(item, index) {&nbsp; &nbsp; &nbsp; &nbsp; return function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTimeout(function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; item.fn();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (index + 1 < wrapList.length) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wrapList[index + 1]();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; go()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, item.time)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; })&nbsp; &nbsp; function go() {&nbsp; &nbsp; &nbsp; &nbsp; setTimeout(function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wrapList[0]();&nbsp; &nbsp; &nbsp; &nbsp; }, 1000) // 整体循环的间隔时间&nbsp; &nbsp; }&nbsp; &nbsp; go();})(list)再更新一个写法:&nbsp;let steps = [{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;time: 10,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;function: function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(1)//函数体&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;},{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;time: 20,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;function: function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(2)//函数体&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;},{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;time: 30,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;function: function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(3)//函数体&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;}&nbsp;];&nbsp;function* iterateSteps(steps) {&nbsp; &nbsp; &nbsp;for (var i = 0; i < steps.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var step = steps[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;yield step;&nbsp; &nbsp; &nbsp;}&nbsp;}&nbsp;var execute = iterateSteps(steps);&nbsp;var value = execute.next().value;&nbsp;(function loop() {&nbsp; &nbsp; &nbsp;setTimeout(function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;value.function();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;value = execute.next().value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;loop();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTimeout(function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; execute = iterateSteps(steps);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value = execute.next().value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loop();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, rnd(5,10)*3600)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;}, value.time)&nbsp;})()&nbsp;//随机数function rnd(n,m)//[n,m]{return parseInt(Math.random()*(m-n+1)+n);};//上promise 的写法。。(某vue群网友帮忙)var list = [{ // 按顺序循环执行的函数列表&nbsp; fn: function() {&nbsp; &nbsp; console.log(1)&nbsp; },&nbsp; time: 1000}, {&nbsp; fn: function() {&nbsp; &nbsp; console.log(2)&nbsp; },&nbsp; time: 1000}, {&nbsp; fn: function() {&nbsp; &nbsp; console.log(3)&nbsp; },&nbsp; time: 2000}, {&nbsp; fn: function() {&nbsp; &nbsp; console.log(4)&nbsp; },&nbsp; time: 3000}];var i = 0, length = list.length;var promise = new Promise(function (resolve, reject) {&nbsp; try {&nbsp; &nbsp; setTimeout(function () {&nbsp; &nbsp; &nbsp; list[0].fn();&nbsp; &nbsp; &nbsp; resolve(0)&nbsp; &nbsp; }, list[0].time)&nbsp; } catch (e) {&nbsp; &nbsp; reject(e);&nbsp; }})function gen() {&nbsp; promise.then(function (value) {&nbsp; &nbsp; if (i < length - 1) {&nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; &nbsp; promise = new Promise(function (resolve, reject) {&nbsp; &nbsp; &nbsp; &nbsp; setTimeout(function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list[i].fn();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resolve(value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gen();&nbsp; &nbsp; &nbsp; &nbsp; }, list[i].time)&nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }&nbsp; })}gen();// 网友帮忙。 async await 写法。【简洁。。直观。】function generatePromise(item) {&nbsp; return new Promise(function (reslove,reject) {&nbsp; &nbsp; setTimeout(function () {&nbsp; &nbsp; &nbsp; reslove(item.fn());&nbsp; &nbsp; },item.time)&nbsp; })}async function asyncList() {&nbsp; for (var i = 0;i< list.length;i++) {&nbsp; &nbsp; await generatePromise(list[i]);&nbsp; }}asyncList();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript