0

I have been trying to make this code work on nightmarejs

new Nightmare({
    show: false
}).goto('http://www.example.com')
.inject('js', 'jquery-3.1.0.js')
.evaluate(() = > {
interval = setInterval(go, 2000);
function go() {
    clearInterval(interval);
}
})
.catch(function (error) {
        console.error('Search failed:', error);
    });

Why would this code never exit?

UPDATE

If I use the .end() method like this:

new Nightmare({
    show: false
}).goto('http://www.example.com')
.inject('js', 'jquery-3.1.0.js')
.evaluate(() = > {
interval = setInterval(go, 2000);
function go() {
    if (condition)
    clearInterval(interval);
}
})
.end() <-----
.catch(function (error) {
        console.error('Search failed:', error);
    });

Then the program exists without letting the evaluate function finish

RetroCode
  • 342
  • 5
  • 14
  • 1
    I really do not know the library, but aren;t you supposed to return something in evaluate? And why are you treating Interval like Timeout? – epascarello Sep 01 '16 at 11:06
  • 1
    Have you tried [end](https://github.com/segmentio/nightmare#end) method? – G07cha Sep 01 '16 at 11:09
  • I mean I tried it with return as well , same thing – RetroCode Sep 01 '16 at 11:09
  • It seems that if I use end() method , then it exists without waiting for evaluate to finish – RetroCode Sep 01 '16 at 11:28
  • @KonstantinAzizov any leads? – RetroCode Sep 01 '16 at 16:30
  • I think the best option will be to use one of the [`wait`](https://github.com/segmentio/nightmare#waitms) functions because `evaluate` waits for synchronized return and as result, doesn't wait for end of interval. – G07cha Sep 01 '16 at 16:32
  • but how can I know beforehand how much time to wait ? There should be a cleaner option – RetroCode Sep 01 '16 at 17:12
  • @RetroCode Just to be sure, you want to wait for some condition to be true on the page, right? If so, then this is a duplicate of http://stackoverflow.com/questions/36723849/nightmare-conditional-wait – Artjom B. Sep 01 '16 at 19:28

0 Answers0