我正在使用PhantomJS运行JavaScript。有一个函数phantom.exit()
接受退出代码作为第一个参数。
以下是它的工作原理。。。
var page = require("webpage").create(),
$ = require("jquery")
page.open("http://127.0.0.1:8081/", function (status) {
var title = page.evaluate(function() {
return $("title").text()
})
phantom.exit(-1)
})
返回的退出代码为-1或255。
我的问题是,当我的函数被调用时,我需要退出脚本。我的函数包含phantom.exit(-1)
。
var page = require("webpage").create(),
$ = require("jquery")
function foo() {
phantom.exit(-1)
}
page.open("http://127.0.0.1:8081/", function (status) {
var title = page.evaluate(function() {
return $("title").text()
})
foo()
phantom.exit()
})
问题是foo()
函数中的phantom.exit(-1)
未被调用,并且退出代码为0。
有一种变通方法可以检查函数中的返回值并手动调用phantom.exit()
,但这并不是我想要的……非常感谢您的帮助!
您对phantom.exit
输出的foo
函数调用可能被page.open
回调函数所覆盖。
因此,调用foo()
将退出代码设置为-1。
然后,在不带参数的情况下调用phantom.exit
,将退出代码重新设置为0。
因为phantom本身可能在执行完所有操作后返回退出代码,所以得到了0,没有-1的符号。