setTimeout ignores timeout when called from page.includeJs() callback #10832
Comments
vasofton...@googlemail.com commented:
|
rob.playford@gmail.com commented:
|
ByteGems...@gmail.com commented:
|
diegogu...@gmail.com commented:
|
i still have this in phantomjs 1.9 with ubuntu x64 |
Still not fixed in phantomjs 1.9.0 with Archlinux(Linux 3.8.5-1). |
This does work for me on Windows 7 (64-bit) with PhantomJS 1.9.0 (downloaded binary). Is it only a bug on Linux OSes now? Haven't seen any MacOSX users chime in either. |
I just tested with 1000 setTimeout()'s in includeJs()'s callback environment, and about 80% failed (the timeout value seems to become 0). PhantomJS 1.9.0 in ArchLinux. |
I think there are some very very very rare occasions when setTimeout doesn't work in main context. I've just tried different combinations of So perhaps it has to do something with the closures or callbacks or something else
|
@jahggler: You have an important typo: |
omg thanks, i thought it should display undefined or something, thanks a lot! |
@dotcink: Did you use a different repro script than the OP did? |
@JamesMGreene Here is my script test.js:
Then output to test.log:
which shows that all 4000
shows that
shows that all 1000
shows that only 192 (about 20%) |
setInterval produces a similar result. It runs 1 time without waiting and doesn't run again. I'm on XP using 1.9 Example var page = require('webpage').create(),
pageLoadDelay = 700,
loadInProgress = false;
page.onConsoleMessage = function(msg) {
console.log(msg);
};
page.onLoadStarted = function() {
loadInProgress = true;
};
page.onLoadFinished = function() {
console.log("Page Loaded")
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js", function(){
console.log("Javascript Included");
var count = 0;
setInterval(function() {
if(count <= 20)
console.log("Interval" + count++)
else
phantom.exit();
}, pageLoadDelay);
});
loadInProgress = false;
};
page.open("google.com");
|
@curtisturner, your example works fine for me on ubuntu x64 with v1.9 (binary) |
We've noticed this happen outside of |
I have also experienced the issues outlined above using version 1.9.1 in both setTimeout and setInterval - the latter of which kills the nice waitFor function outlined in some of the examples. Pretty frustrating. I can't share all of the code, but it looks something like the following:
|
Confirmed in 1.9.2 on Win 7, in main context. |
setTimeout & setTimeInterval getting ignored in win 7/64 with phantom js 1.9.2. Is there a fix available for this? Any chance the fix is missing in the windows version? |
Nope, I have the same problem on a Mac. |
Thus why the issue is still open. |
We are also facing same issue with phantomjs 1.9.1 on Ubuntu 12.04 32 bit |
In case anyone else is struggling with this, I found that wrapping a setTimeout in another setTimeout works: Example:
|
I can confirm the issue in Phantom.js 1.9.2 on Fedora. It looks like setTimeout is ignored both in the main context and page.evaluate calls. |
Trying a patch suggested at the phantomjs github repo issues ariya/phantomjs#10832
Any news about this? Anyone with time to check this out? just tested @emergedennis 's patch and didn't work either... |
a year later and, still not working on the main context and page evaluate (centos 6.4) |
I checked @emergedennis 's patch and it worked.
I use PhantomJS 1.9.7 for windows on windows8 |
I had the script for the phantomjs which works perfectly without any timeout workarounds on my mac: // checking if the result is shown on the page
(function() {
var dfd = new $.Deferred();
// how many times to check before we go further
var checkNumber = 5;
var checkInterval = setInterval(function() {
checkNumber--;
var resultSuccess = page.evaluate(function() { return $('.class').is('*'); });
var resultEmpty = page.evaluate(function() { return $('div.class:contains(text to check)').is('*'); });
if (resultSuccess || resultEmpty) {
clearInterval(checkInterval);
dfd.resolve();
} else if(checkNumber == 0){
clearInterval(checkInterval);
dfd.reject();
}
}, 5000);
return dfd;
})().then(/*success*/ function(){
var name = page.evaluate(function(){
return $('.class').text();
});
page.render('results_page.png');
var endTime = (new Date).getTime();
console.log(JSON.stringify({'status': 'success', 'name': name, 'executionTime': ((endTime - startTime)/1000)}));
phantom.exit();
}, /*failure*/ function(){
console.log(JSON.stringify({'status': 'error', 'message': 'Unable to submit'}));
phantom.exit();
}); (it uses the jQuery deferred objects) However, today, when I tried to do the same on my mac but in the other script - I found that setInterval fails :( Current solution is to warp it in the setTimeout, but I'm not a fond of this. p.s. I found why it was working, because before I called the page.open 2nd time (that was required by the logic of the script). More info - below. |
Here is another variant of making the setInterval and setTimeout work. The minimum viable phantomjs script looks this way: phantom.page.injectJs('./jquery-1.11.1.min.js');
var page = require('webpage').create();
page.open('http://yandex.ru', function() {
page.includeJs("http://code.jquery.com/jquery-1.11.0.min.js", function() {
(function() {
var dfd = new $.Deferred();
page.open('http://yandex.ru', function(status) {
dfd.resolve();
});
return dfd.promise();
})().then(function() {
var checkNumber = 5;
var checkInterval = setInterval(function() {
checkNumber--;
console.log(checkNumber);
if (checkNumber == 0) {
clearInterval(checkInterval);
console.log("exit");
phantom.exit();
}
}, 5000);
});
});
}); The funny part of it is, that as soon as we would remove another page.open from the code - it would stop working. So, somehow, 2nd page.open, makes the setTimeout and setInterval works. This solutions is way worth from the setTimeout(function(){},1); , but both of them are illogical, because I expect that behaviour to work without any workarounds. Would hope that in the next versions of the phantomjs it would work by default. p.s. |
I have this issue as well. No delay on the timeout callback. |
@simonklb Which version of phantomjs are you working with? I thought it was already resolved in one of the recent releases. |
@apendua I got the build in the Ubuntu repo. Version 1.9.0. Which version is this solved in? |
In that case you're using pretty outdated version. I guess you should try |
Looks like it's working, thanks! This should perhaps be marked as resolved then? |
Just a note for users. 1.9.8 still had the setTimeout bug when I tried it. I updated to 2.0 and setTimeout works properly. |
The setTimeout still doesn't work on my Fedora 21, phantomjs version 2.0 |
Still happening for me - PhantomJS 2.0 built from source on RHEL 6.3. |
@ashafer01 |
Still no working for me with Win 7 and phantom 2.0 |
Still facing the issue on Mac with phantomjs 2.0.1-dev!! |
Bump. I have some code under test which uses
The exact same spec passes 100% of the time when I have Capybara execute in Firefox instead of PhantomJS. |
In fairness, I have tried to set up a minimal example, and could do it. Something is particular to my code (websockets?) which is causing the problem. |
Wow, this bug has been open for 4 years and still no resolution? |
Solution:
Double timeouts and it works ¯_(ツ)_/¯ |
@frazras I remember using this fix ~3 years ago. It worked for some time and then it broke again with another release of |
I have been struggling with this issue as well. Did some quick tests to see if it was related to load. Which version of PhantomJS are you using? Tip: run 'phantomjs --version Which operating system are you using? Did you use binary PhantomJS or did you compile it from source? What steps will reproduce the problem? Construct a HTML document with a number of Divs. I did it in PHP (see attachment) start a setTimeout loop and have PhantomJS open the html we just generated, vanilla page.open logic:
The count is always 1. I then increase the number of Divs and measure how long it takes PhantomJS to open on localhost + memory consumption. CPU is locked at 100% when opening the page, i get memory from bash. Here are the results: As you can see there is not a linear relationship between the size of the document and load time or memory consumption. Also it appears page.open blocks all other processing. Opening HTML with ~1200 divs causes PhantomJS to consume 10GB+ memory and almost 5 minutes just to open the page on a gigabit network. This is the root cause of many exceptions when opening pages. I do not know C++ or i would help find the structure that causes memory and execution time to grow exponentially.
|
Due to our very limited maintenance capacity (see #14541 for more details), we need to prioritize our development focus on other tasks. Therefore, this issue will be automatically closed. In the future, if we see the need to attend to this issue again, then it will be reopened. Thank you for your contribution! |
chri...@parastudios.de commented:
Disclaimer:
This issue was migrated on 2013-03-15 from the project's former issue tracker on Google Code, Issue #832.
🌟 6 people had starred this issue at the time of migration.
The text was updated successfully, but these errors were encountered: