Javascript 包含有时间间隔问题的JS
源代码Javascript 包含有时间间隔问题的JS,javascript,phantomjs,Javascript,Phantomjs,源代码 var page = require('webpage').create(); page.onConsoleMessage = function(msg) { console.log(msg); }; page.open("http://info.finance.yahoo.co.jp/fx/", function(status) { if ( status === "success" ) { interval = setInterval(functi
var page = require('webpage').create();
page.onConsoleMessage = function(msg) {
console.log(msg);
};
page.open("http://info.finance.yahoo.co.jp/fx/", function(status) {
if ( status === "success" ) {
interval = setInterval(function(){
console.log("executed");
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
var result = page.evaluate(function() {
console.log($("#USDJPY_bid").text());
});
});
}, 3000);
}
});
它起作用了
>phantomjs --web-security=false sample.js
executed
102.6
executed
102.6
102.6
executed
102.6
102.6
102.6
我想通过jQuery重新加载数据。首先,它连接到网站和,并在保持连接的情况下重复执行jQuery
每次调用console.log时,它的数量都会增加。它只发生在Includejs内部。我只需要调用一次。如果仔细查看,您会发现jQuery已经包含在页面中,因此您不需要自己包含它。这就足够了:
page.open("http://info.finance.yahoo.co.jp/fx/", function(status) {
if ( status === "success" ) {
interval = setInterval(function(){
var result = page.evaluate(function() {
console.log($("#USDJPY_bid").text());
});
}, 3000);
}
});
如果仍希望包含jQuery版本,则只应执行一次:
这在我的机器上只起作用,没有-web security=false标志。奇怪的是,我不得不将includeJs与setInterval解耦。这似乎是一个bug。清除间隔后,不记录丢失的hogehoge。看起来没有什么悬而未决的问题。您可以在上报告此错误。感谢您的重播。我不擅长使用英语。我希望输出是“HogeHogeFoobar HogeHogeHoogeFoobar…”。可以吗?你是说“hogehoge”是臭虫?不,这不是批评。我想说的是,这确实是错误的行为和错误。我很想知道你为什么需要这个。你有多个脚本需要加载吗?我知道这种行为是错误的。我想通过jQuery重新加载数据。首先连接到网站,并重复执行jQuery以获取新数据。但是每次输出的次数都会增加。重新加载jQuery库不会将新数据加载到页面。您必须与页面交互,比如单击链接/按钮或直接向服务器发送AJAX请求。
page.open("http://info.finance.yahoo.co.jp/fx/", function(status) {
if ( status === "success" ) {
var loaded = false;
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
loaded = true;
});
setInterval(function(){
if (!loaded) return;
page.evaluate(function() {
console.log($("#USDJPY_bid").text());
});
}, 3000);
}
});