admin管理员组文章数量:1315054
I am trying to write a simple polling application using NodeJS. I'm wanting to write an EventEmitter
that performs an action on a timer and emits events based on the result of that periodic action.
I'm starting by creating my own object and inheriting from EventEmitter
. I kick off a timer using setInterval
, and specify the method to call after the timer elapses. Within the timer callback method, I want to reference a variable of the object that I've created, but this
does not appear to refer to the object.
How can I reference my variable in this method? Here is my code:
var util = require('util'),
events = require('events'),
timers = require('timers'),
redis = require('redis');
// define worker object
var JobPoller = function () {
// inherit event emitter
events.EventEmitter.call(this);
// save reference to database
this.db = redis.createClient();
// start main loop
this.interval_id = timers.setTimeout(this.check, 1000);
};
JobPoller.prototype.check = function () {
// pop a job off the queue if possible
this.db.rpop('pdf-job-queue', function (err, result) {
if (err != null)
this.emit('error', err);
if (result != null)
this.emit('job', JSON.parse(result));
// check for more jobs
this.interval_id = timers.setTimeout(this.check, 1000);
});
};
// inherit from event emitter
util.inherits(JobPoller, events.EventEmitter);
// export the poller instance
module.exports = new JobPoller;
I am trying to write a simple polling application using NodeJS. I'm wanting to write an EventEmitter
that performs an action on a timer and emits events based on the result of that periodic action.
I'm starting by creating my own object and inheriting from EventEmitter
. I kick off a timer using setInterval
, and specify the method to call after the timer elapses. Within the timer callback method, I want to reference a variable of the object that I've created, but this
does not appear to refer to the object.
How can I reference my variable in this method? Here is my code:
var util = require('util'),
events = require('events'),
timers = require('timers'),
redis = require('redis');
// define worker object
var JobPoller = function () {
// inherit event emitter
events.EventEmitter.call(this);
// save reference to database
this.db = redis.createClient();
// start main loop
this.interval_id = timers.setTimeout(this.check, 1000);
};
JobPoller.prototype.check = function () {
// pop a job off the queue if possible
this.db.rpop('pdf-job-queue', function (err, result) {
if (err != null)
this.emit('error', err);
if (result != null)
this.emit('job', JSON.parse(result));
// check for more jobs
this.interval_id = timers.setTimeout(this.check, 1000);
});
};
// inherit from event emitter
util.inherits(JobPoller, events.EventEmitter);
// export the poller instance
module.exports = new JobPoller;
Share
Improve this question
edited Nov 28, 2018 at 23:21
pushkin
10.2k16 gold badges62 silver badges107 bronze badges
asked Mar 2, 2012 at 4:18
RyanRyan
7,95711 gold badges65 silver badges118 bronze badges
1 Answer
Reset to default 15this.check
is just a simple function, the value of this
inside that function will be determined when the function is called.
Node should support bind
so you can bind the function to your desired this
like this:
this.interval_id = timers.setTimeout(this.check.bind(this), 1000);
Alternatively, you can use a closure to manually force this
to be what you want:
var self = this;
this.interval_id = timers.setTimeout(function() { self.check() }, 1000);
本文标签: JavaScriptNodeJSsetTimeout callback method and quotthisquotStack Overflow
版权声明:本文标题:javascript - node.js, setTimeout callback method and "this" - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740813170a2290150.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论