2

js 如何优化这种情形: 无限状态切换

 2 years ago
source link: https://www.v2ex.com/t/823157
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  JavaScript

js 如何优化这种情形: 无限状态切换

  FaiChou · 12 小时 24 分钟前 · 631 次点击
function start() {
  var screen = captureScreen();
  // 出现弹框, 干掉弹框
  if (screen.find(ALERT_BUTTON) {
  	click(ALERT_CLOSE_BUTTON);
    start();
  }
  // 如果有返回按钮, 点击返回
  if (screen.find(BACK_BUTTON) {
    click(BACK_BUTTON);
    start();
  }
  // 如果不在首页(首页没有高亮选中), 点击回到首页
  if (screen.find(HOME_BUTTON_UNHIGHLIGHT) {
    click(HOME_BUTTON_UNHIGHLIGHT);
   	start(); 
  }
  // 在首页了, 有“喜欢按钮”
  if (screen.find(LIKE_BUTTON) {
    click(LIKE_BUTTON);
    swipeNextVideo();
    sleep(5000);
    start();
  }
}

setInterval(function() {
  if (currentPackage() !== 'PACKAGE_NAME') {
    launch('PACKAGE_NAME');
  }
}, 1000*60*10) // 10 分钟检查一次程序是否被杀死, 程序不在运行就重新打开

start();

以上是脚本程序的大概逻辑:

打开某视频软件, 如果有弹框, 干掉弹框, 如果不在首页, 返回首页, 刷视频点赞, 然后继续下一轮

程序使用了递归, 如果运行时间过长, 内存占用会变大. 有什么方法可以优化这一点?

我尝试过使用 whilecontinue:

while (true) {
  if (condition1) {
    doSometing2();
    continue;
  }
  if (condition2) {
    doSometing2();
    continue;
  }
  ..
}

死循环会让外面的 setInterval 不被执行.

还尝试过使用 generator 来做, 配合 redux-saga, 但这个框架不支持 es6, 在使用 babel 转译时候, 遇到一堆问题, 就跑不通了.

所以除了 以上几种方法, 还有什么方法可以优化吗?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK