Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8889209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:12:21+00:00 2026-06-14T22:12:21+00:00

I need to make a loop of 3 times and 2 seconds in between

  • 0

I need to make a loop of 3 times and 2 seconds in between each iteration. I tried these 3 options:

Option 1

var casper = require('casper').create({
    verbose: false,
    logLevel: 'debug'
});

casper.start("http://google.com");

casper.on('remote.message', function(msg) {
    this.echo('remote message caught: ' + msg);
})

casper.thenEvaluate(function() {
    var x = 0;
    var intervalID = setInterval(function () {

       console.log("Using setInternal " + x);

       if (++x === 3) {
           window.clearInterval(intervalID);
       }
    }, 2000);

});

casper.run();

Observation: Nothing appeared because the script ended right away before the first setInterval being called.

Option 2

Replaced thenEvaluate() with then() below

for (i=0; i<3; i++) {
    this.wait(2000);
    this.echo('Using this.wait ' + i);
}

Observation: It outputs 3 times right away and then a long wait since this.wait() is async. This is not what I want because I want a delay in between.

Option 3
Replace the part in then() with this below. I was thinking about doing a recursive call to waitFunc() after each wait() being called.

var count = 0;
var waitFunc = function() {
    this.wait(2000, function() {
        if (count < 3) {
            casper.echo('Using this.wait ' + count);
            count++;
            waitFunc();
        }
    });

};

Observation: Nothing printed out on the screen.

So my question is: How to make this.wait or setInterval works in a loop of 3 times like this case?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:12:22+00:00Added an answer on June 14, 2026 at 10:12 pm

    Here’s a sample implementation to solve your problem:

    var casper = require('casper').create();
    var last, list = [0, 1, 2, 3];
    
    casper.start("http://google.fr/", function() {
        this.echo('google');
    });
    
    casper.on('remote.message', function(msg) {
        this.echo('remote message caught: ' + msg);
    });
    
    casper.thenEvaluate(function() {
        window.x = 0;
        var intervalID = setInterval(function() {
           console.log("Using setInternal " + window.x);
           if (++window.x === 3) {
               window.clearInterval(intervalID);
           }
        }, 500);
    });
    
    casper.each(list, function(self, i) {
        self.wait(500, function() {
            last = i;
            this.echo('Using this.wait ' + i);
        });
    });
    
    casper.waitFor(function() {
        return last === list[list.length - 1] && 3 === this.getGlobal('x');
    }, function() {
        this.echo('All done.').exit();
    });
    
    casper.run(function() {});
    

    Sample output:

    $ casperjs test.js
    google
    remote message caught: Using setInternal 0
    Using this.wait 0
    remote message caught: Using setInternal 1
    Using this.wait 1
    remote message caught: Using setInternal 2
    Using this.wait 2
    Using this.wait 3
    All done.
    $
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to make a loop that runs for 3 times and a switch
I need to make an Ajax call for every x seconds, to read the
I need to convert my string into int array. so i make a loop
I need make some action (dump statistical data) before the Dart program ends. The
I need make all of my posts update. I use bulk upload for store,
Need to make certain Ruby strings in my program to be immutable. What is
I need to make a administrative GUI with lots of functionality like lists, forms,
I need to make a moderate system like the one in fmylife.com. Basically the
I need to make a point in time branch, when we release a new
I need to make efficient d-dimensional points searching and also make efficient k-NN queries

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.