CasperJS setInterval,同步执行

时间:2013-05-24 15:16:15

标签: javascript casperjs

我想用CasperJS模拟鼠标漂移。为此,我想使用setInterval。间隔在另一个文件中定义,只需使用require()来获取它。我可以执行它,但Casper不等待间隔完成。间隔运行大约200ms,然后Casper继续执行代码。当drift()完成后,它会调用“完成”的回调。

我如何告诉CasperJS等到执行完成()?

casper.waitFor(function () {
  var result = drift({
    ...
    finished: function (x,y) {
      //casper.test.comment('up ' + x + ' ' + y);
      that.mouse.up(x, y);
    }
  }, window);

  return result;
}, function () { /** test here... **/ });

function drift (options, window) {
  interval = setInterval(function () {
    if (counter <= limit) {
      x += stepX;
      y += stepY;
      //call the way callback    
      options.way(x, y);
      counter += 1;
    } else {  
      options.finished(x, y);
      ...
      window.clearInterval(interval);
      returnVal = true;
    }
  }, 50);
  return returnVal;
}

完整代码在这里:https://github.com/schickling/Overscroll/blob/testing/test/drift.js 在这里:https://github.com/schickling/Overscroll/blob/testing/test/modules/drift-move.js

0 个答案:

没有答案
相关问题