我正在使用 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()
,但这不是我想要的......非常感谢任何帮助!
最佳答案
有一种可能性是,您的 foo
函数对 phantom.exit
输出的调用被 page.open
回调覆盖。
因此,调用 foo()
将退出代码设置为 -1。
然后,调用不带参数的phantom.exit
,将退出代码设置回0。
因为 phantom 本身可能会在执行完所有内容后返回退出代码,所以您得到 0,没有 -1 的迹象。
关于javascript - 从另一个函数调用时 phantom.exit(-1) 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13837916/