I am writing a Javascript class to cycle through a number of ticks and call a function at specified ticks. However, I am having a problem with my prototyping of the Javascript Class. The main count variable appears as undefined or Nan and one of the functions (methods) is apparently "not a function". Am just perplexed. Any pointers would be great.
Here is a JSFiddle: https://jsfiddle.net/hp5dj665/ but here is the code in question:
<script>
function tickStep(tickInMilliSeconds){
this.tickCount = 0; //just used as a counter
this.tickMax = 48; //highest number of ticks before it ends
this.monitorTextInputId = "";
this.paused = false;
this.tickMilliSeconds = tickInMilliSeconds;
this.tickRepeat = true; //when reaching the end start again at the beginning
this.eventArray = new Array();
this.setint; //the set time out variable
}
tickStep.prototype = {
constructor: tickStep,
monitorTick:function(){
console.log(this.tickCount);
/* if(this.monitorTextInputId.length>0){
document.getElementById(this.monitorTextInputId).value = this.tickCount;
}*/
},
tick:function(){
if(!this.paused){
console.log("HERE: "+this.tickCount); // WHY IS THIS NaN ??? <---------------
this.tickCount++;
if(this.tickCount>this.tickMax && this.tickRepeat){
this.tickCount = 0;
}
console.log("tick: "+this.tickCount);
if(this.tickCount>this.tickMax && !this.tickRepeat){
this.stop();
return false;
}
this.monitorTick(); // <!----------------- WHY DOES THIS SAY IT IS NOT A FUNCTION?
if(typeof this.eventArray[this.tickCount] !== "undefined"){
if(this.isAFunction(this.eventArray[this.tickCount])){
eval(this.eventArray[this.tickCount]);
}
}
}else{
console.log("Paused...");
}
},
isAFunction:function(functionalCall){
if(functionName.indexOf("(")){ //remove the brackety stuff
functionName = functionName.substring( 0,functionName.indexOf("(") );
}
console.log("Testing for function: "+functionName);
return typeof(functionName) === typeOf(Function);
},
start:function(){
console.log("STARTING");
if(!this.tickMilliSeconds>0){
this.tickMilliSeconds = 1000; //default to 1 second
console.log("defaulting to 1 tick = 1000ms")
}
console.log("Tick duration: "+this.tickMilliSeconds);
this.setint = window.setInterval(this.tick,this.tickMilliSeconds);
},
stop:function(){
console.log("STOPPING");
clearInterval(this.setint);
},
restart:function(){
console.log("RESTARTING");
this.stop();
this.tickCount =0;
this.start();
},
pause:function(){
this.paused = !this.paused;
},
addEvent:function(tickValue,funcCall,params){
this.eventArray[this.tickValue] = funcCall+"("+params+")";
},
removeEvent:function(tickValue){
this.eventArray[this.tickValue] = null;
}
} //end of tickStep prototype
var seq = new tickStep();
seq.monitorTextInputId = "tb";
var myFunction = function(){
console.log("myFunctionCalled");
}
seq.addEvent(2,myFunction,"");
seq.start();
</script>
So 1. Why does the "this.tickCount" == Nan or undefined inside the Tick function 2. Why is this.monitorTick() apparently not a function when this.tick() is a function?
Stuff may break after that as I can't get past that stage - but I would like those two queries sorted out so I can progress. Thanks
UPDATE
Just for completeness sake, following a couple of the comments I thought I would post that the addEvent function is now:
addEvent:function(tickValue,funcCall,params){
this.eventArray[tickValue] = Array(funcCall,params);
},
and tick is now:
tick:function(){
if(!this.paused){
this.tickCount++;
if(this.tickCount>this.tickMax && this.tickRepeat){
this.tickCount = 0;
}
if(this.tickCount>this.tickMax && !this.tickRepeat){
this.stop();
return false;
}
this.monitorTick();
if(typeof this.eventArray[this.tickCount] != "undefined"){
this.eventArray[this.tickCount][0](this.eventArray[this.tickCount][1]);
}
}else{
console.log("Paused...");
}
},
Binding "this" on the setInterval call solved the initial problem. Thanks everyone.