我正在做一些Web自动化操作,当我在chrome控制台中运行代码时,它会逐步执行这些步骤,因此页面无法在下一个事件发生之前就位。我试图添加一个延迟,但是它不能使速度变慢。现在,我注意到,如果我运行细分到步骤函数step1(),step2(),step3()等的代码,它可以工作,但是当所有步骤都在同一个js文件中直接运行时却不能,因为我们得到了同样的计时问题,其代码运行速度超过了网页的响应速度。如何运行多个js文件,Step1()文件,Step2()文件,Step3()文件等,以解决此计时问题?
等待代码
//Wait function
function wait(ms){
//alert('Waiting');
ms += new Date().getTime();
while (new Date() < ms){};
};
当前代码布局
//running section
//Step 1
var fruits = step1();
//Wait 2 Seconds
wait(2000);
//Step 2
step2(fruits);
//Wait 2 Seconds
wait(2000);
//Step 3
step3();
//Wait 2 Seconds
wait(2000);
//Step 4
step4();
目标:
运行:Step1File.js
运行:Step2File.js
运行:Step3File.js
运行:Step4File.js
使用异步等待。将每个函数声明为一个异步函数,然后将其置于await
代码布局中的每个函数调用之前。
// step 1
async function step1() {
// do some stuff
}
对所有功能重复
// running section
async function runTheStuff() {
await step1()
await step2()
await step3()
}
runTheStuff()
对此进行了严格简化,但是应该可以帮助您入门。aync/await
-学习,爱它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句