XHR sleeps 1 second

11 views
Skip to first unread message

j...@webmaster.ms

unread,
May 13, 2012, 12:57:40 PM5/13/12
to phantomjs
Hi.

I am using PhantomJS for Windows, version from here
http://code.google.com/p/phantomjs/downloads/list

I noticed that XMLHttpRequest takes at least 1 second to perform a
request, even if the target server is on localhost.

Below is a testcase using syncronous XHR (test1.js)
But asyncronous XHR has the same problem.
The delay from xhr.send() to first callback is at least 1 second.

The same problem is reproducible even if the XHR code is on the loaded
html page (test2.js and 1.html)

At first glance it may look as a server issue, but it is not.
I checked various http servers, and also it is not reproducible with
ordinary browsers.


=== test1.js ===
var page = require('webpage').create();

page.onConsoleMessage = function(msg) { console.log(msg); };

page.open("http://localhost:6688/", // fast local server (embedded in
phantomjs won't work)
function (status) {
if (status !== 'success') {
console.log('FAIL to load the address');
}
page.evaluate(function() {
for (i=0; i<30; i++) {
console.log("about to create XHR");
var xhr=new XMLHttpRequest();
console.log("about to call xhr.open");
xhr.open("GET", "http://localhost:6688/", false);
console.log("about to call xhr.send");
xhr.send(null);
console.log("returned from xhr.send");
}
});
phantom.exit();
});
===



=== test2.js ===
var page = require('webpage').create();
page.onConsoleMessage = function(msg) { console.log(msg); };
page.onLoadFinished = function (status) { phantom.exit(); };
page.open('1.html');
===

=== 1.html ===
<script>
for (i=0; i<300; i++) {
console.log("about to create XHR");
var xhr = new XMLHttpRequest();
console.log("about to call xhr.open");
xhr.open('GET', 'http://localhost:6688/', false);
console.log("about to call xhr.send");
xhr.send(null);
console.log("returned from xhr.send");
}
</script>
===

Reply all
Reply to author
Forward
0 new messages