I'm trying to make a simple task queue with setInterval with a linked-list. I created a class with linkedlist and a setInterval function will keep calling a member function to consume the job.
function job_queue(){
this.job = null;
this.pointer = this.job;
this.job_dispatcher = null;
this.length = 0;
}
job_queue.prototype.add_job = function( job ){
if( this.job == null ){
console.log('1st');
this.job = {
job:job,
next:null
};
this.pointer = this.job;
this.length = 1;
}else{
console.log('2nd');
this.pointer.next = {
job:job,
next:null
};
this.pointer = this.pointer.next;
this.length++;
}
};
job_queue.prototype.event_handler = function(){
if( typeof this.job['job'] == 'undefined'){
console.log('??');
}
if( this.job.job != null ){
console.log('hi');
this.job.job();
this.job = this.job.next();
}
}
job_queue.prototype.start_dispatch = function(){
if( this.job_dispatcher == null ){
console.log( this.event_handler );
this.job_dispatcher = setInterval( this.event_handler,1000);
}
}
var jq = new job_queue();
function a(){
console.log('hi');
};
function b(){
console.log('hi2');
}
jq.add_job(a);
jq.add_job(b);
jq.add_job(a);
jq.start_dispatch();
However, when the event_handler function gets called , the program crashes with the log
if( typeof this.job['job'] == 'undefined'){
It seems like it can not access the member variable by calling member function with setInterval. I would like to ask what exactly happened with these lines of code and how can I achieve the goal?