2012-04-25 116 views
2

我正在用node.io构建一个刮板。node.io:作业完成后,再次执行

我想要报废的页面每分钟都有新内容。我希望每分钟都能一次又一次地完成我的工作。 (好吧,我能做到这一点有一个bash脚本,但我想留在JavaScript) 这是一个基本的工作:

var nodeio = require('node.io'), options = {timeout: 10}; 

exports.job = new nodeio.Job(options, { 
    input: ['hello', 'foobar', 'weather'], 
    run: function (keyword) { 
     this.getHtml('http://www.google.com/search?q=' + encodeURIComponent(keyword), function (err, $) { 
      var results = $('#resultStats').text.toLowerCase(); 
      this.emit(keyword + ' has ' + results); 
     }); 
    } 
}); 

我怎么能这样做呢?我在node.js的初学者,我试过的setInterval周围的工作(:没有成功

+0

关于使用一段时间(真)和setTimeout的什么(60000) – ControlAltDel 2012-04-25 15:40:43

+0

@ user1291492:作业从不开始 – 2012-04-25 15:46:22

+0

那么你甚至测试过你的代码?我看到node.io网站上有很多教程。也许这将是开始的地方 – ControlAltDel 2012-04-25 15:50:23

回答

3

试试这个(与 “节点<myfile.js>”,而不是 “node.io <myfile.js>”)运行:

var nodeio = require('node.io'), options = {timeout: 10}; 
var job = { 
    input: ['hello', 'foobar', 'weather'], 
    run: function (keyword) { 
     this.getHtml('http://www.google.com/search?q=' + encodeURIComponent(keyword), function (err, $) { 
     var results = 'test';//$('#resultStats').text.toLowerCase(); 
     this.emit(keyword + ' has ' + results); 
     }); 
    } 
}; 

setInterval(function(){ 
    nodeio.start(new nodeio.Job(options, job), options, function(){}); 
}, 5000); 

你正在运行到的问题是在node.io以下代码块退出的节点,当你运行作业时不提供回调:

//Default behaviour is to exit once the job is complete 
callback = callback || function (err) { 
    if (err) { 
     utils.status.error(err); 
    } 
    process.exit(); 
}; 
相关问题