title: Why the statement inside setInterval function is not getting executed in Phantomjs?
body: <p>I am trying to Login into website using Phantomjs. When I click loginButton after filling form elements, I was trying to find some element id inside setInterval function as I want page to load first (gave 2 seconds) and then only search for the element but the problem here is : The statements inside my setInterval are not getting executed. Here is the phantomjs code : </p>
  
  <pre><code>var url = "http://www.someUrl.com";
  var page = require('webpage').create();
  
  page.onConsoleMessage = function(msg, lineNo, sourceId){
      console.log('console: ' + msg);
  };
  
  page.onLoadFinished = function(status){
      console.log("Loading finished");
      page.render('webpage.png');
  };
  
  page.open(url, function(status){
      page.evaluate(function(){
          arr = document.getElementsByClassName('formLabel');
          if (typeof arr !== 'undefined'){
              arr[0].value = 'Login-id';
              arr[1].value = 'Password';
          }
          document.getElementById('loginButton').click();
          setInterval(function(){
              console.log("Downloading the file");
              document.getElementById('downloadButton').click();
          }, 2000);
      });
  });
  </code></pre>
  
  <p>When I execute the above code, I get the output as :-</p>
  
  <pre><code>Loading finished
  Loading finished
  </code></pre>
  
  <p>For debugging purposes, I wrote <code>console.log('Downloading the file')</code> inside the setInterval function but this statement is not getting executed.
  I also render the webpage as image <code>page.render('webpage.png')</code> to check whether I am able to successfully login or not. And I see that Login is successful. So, I have few questions here :-</p>
  
  <ol>
  <li>Why those statements inside <code>setInterval</code> function are not getting executed?</li>
  <li>Where should I write those statements to make them work correctly?</li>
  </ol>
  
  <p>In fact, when I wrote those statements just after the <code>loginButton.click()</code> statement, I got this error - <strong>TypeError: 'null' is not an object (near 'downloadButton').click();...')</strong>, that means page did not load by that time. Hence, I wrote them inside setInterval function and gave the waiting time as 2 seconds.</p>
  
  <p>PS : There is no problem in my <code>click</code> statement because my <code>loginButton.click</code> is working.</p>
  
  <p>Thanks in Advance.</p>