在react中,我想让task01和task02循环执行,怎么修改?

task01() {

        console.log('人的一生中')

        

        setTimeout(this.task02, 1000)

        

    }



    task02 () {


        var _self = this;

        console.log('两亿')


        setTimeout(_self.task01, 1000)


    }



    task03 () {

        console.log('测试任务三个月')

    }



    componentDidMount() {

            var _self = this;

            console.log('两亿')

            

            var taskStart = function(){


                console.log('任务开始了')

                setTimeout(_self.task02, 1000)


            }


            taskStart();


            //Websocket.finishConnect();

        }

    }

https://img1.mukewang.com/5c88bf8900010f6808000358.jpg

https://img2.mukewang.com/5c88bf8b0001059408000491.jpg

https://img2.mukewang.com/5c88bf8c0001b41208000497.jpg

杨__羊羊
浏览 557回答 2
2回答

尚方宝剑之说

&nbsp; &nbsp;let functionArr = [{task01:function(){console.log('这个是task01')}},{task02:function(){console.log('这个是task02')}}]&nbsp; async function taskStart(functionArr) {&nbsp; &nbsp; for (let i = 0; i <= functionArr.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; let task = await sleep(1000)&nbsp; &nbsp; &nbsp; &nbsp; if(i === 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; functionArr[i].task01()&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(i === 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; functionArr[i].task02()&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (i === 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i = 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; functionArr[i].task01()&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log(functionArr[i])&nbsp; &nbsp; }&nbsp; }&nbsp; const sleep = (time) => {&nbsp; &nbsp; &nbsp; return new Promise((resolve) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTimeout(() => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resolve()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }, time)&nbsp; &nbsp; &nbsp; })&nbsp; }

素胚勾勒不出你

task01() {&nbsp; &nbsp; &nbsp; &nbsp; console.log('人的一生中');&nbsp; &nbsp; &nbsp; &nbsp; setTimeout(this.task02, 1000)&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }task02 () {&nbsp; &nbsp; &nbsp; &nbsp; var _self = this;&nbsp; &nbsp; &nbsp; &nbsp; console.log('两亿');&nbsp; &nbsp; &nbsp; &nbsp; setTimeout(this.task01, 1000)&nbsp; &nbsp; }&nbsp; &nbsp; task03 () {&nbsp; &nbsp; &nbsp; &nbsp; console.log('测试任务三个月');&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; taskStart() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log('任务开始了');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setTimeout(this.task02, 1000);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; componentDidMount() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log('两亿');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.taskStart();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Websocket.finishConnect();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript