auto.js中怎么循环好?我用百度复制的function auto()过一段时间就熄火了,代码就是下边的

function auto(){
//循环代码

//请求截图
if(!requestScreenCapture()){
toast("请求截图失败");
exit();
}

//截图命名为9.png
captureScreen("/sdcard/9.png");
sleep(2000)
// 读取本地图片/sdcard/1.png
var img = images.read("/sdcard/9.png");
// 判断图片是否加载成功
if(!img){
toast("没有该图片");
exit();
}
// 在该图片中找色,指定找色区域为在位置(400, 500)的宽为300长为200的区域,指定找色临界值为4
var point = findColor(img, "#70bdc8", {
region: [1768, 137, 50, 20],
threshold: 4
});
if(point){
toast("找到啦:" );
click(1909,153)
sleep(7000)

}else{
toast("没找到");
}

//多少秒执行一次 上面的参数就填 秒数乘1000 上述 一秒调用一次

setTimeout(auto,120000);
}
auto();

第1个回答  2022-01-30
exit() 执行了。如果我没猜错的话,exit里面有throw语句;或者你里面的某一个函数报错了,报错不是用console.error 而是用了throw语句,或者直接原生报错。改良一下应该是。
function auto() {
//循环代码
try {

//请求截图
if (!requestScreenCapture()) {
toast("请求截图失败");
exit();
}

//截图命名为9.png
captureScreen("/sdcard/9.png");
sleep(2000)
// 读取本地图片/sdcard/1.png
var img = images.read("/sdcard/9.png");
// 判断图片是否加载成功
if (!img) {
toast("没有该图片");
exit();
}
// 在该图片中找色,指定找色区域为在位置(400, 500)的宽为300长为200的区域,指定找色临界值为4
var point = findColor(img, "#70bdc8", {
region: [1768, 137, 50, 20],
threshold: 4
});
if (point) {
toast("找到啦:");
click(1909, 153)
sleep(7000)

} else {
toast("没找到");
}
} catch (e) {
console.error(e)
} finally {
//多少秒执行一次 上面的参数就填 秒数乘1000 上述 一秒调用一次

setTimeout(auto, 120000);
}
}
auto();
还有sleep应该不起作用,js没有sleep方法。正确函数应该是

async function auto() {
//循环代码
try {
//请求截图
if (!requestScreenCapture()) {
toast("请求截图失败");
exit();
}

//截图命名为9.png
captureScreen("/sdcard/9.png");
await new Promise(rs=>{
setTimeout(()=>{
rs("ok")
},2000)
})
// 读取本地图片/sdcard/1.png
var img = images.read("/sdcard/9.png");
// 判断图片是否加载成功
if (!img) {
toast("没有该图片");
exit();
}
// 在该图片中找色,指定找色区域为在位置(400, 500)的宽为300长为200的区域,指定找色临界值为4
var point = findColor(img, "#70bdc8", {
region: [1768, 137, 50, 20],
threshold: 4
});
if (point) {
toast("找到啦:");
click(1909, 153);
await new Promise(rs=>{
setTimeout(()=>{
rs("ok")
},7000)
})
} else {
toast("没找到");
}
} catch (e) {
console.error(e)
} finally {
//多少秒执行一次 上面的参数就填 秒数乘1000 上述 一秒调用一次
auto();
// setTimeout(auto, 120000);
}
}
auto();
相似回答