0

I am trying to set an interval in my JS file and call a function from within it. However, I get an error when trying to call a function from inside of the interval. Here is my code:

Note: I trigger _testFunction() to start the chain at the top of the document.

_testFunction3: function(){
    console.log("Test Function 3 Hit");
},
_testFunction2: function(){
    console.log("Test Function 2 Hit");
},
_testFunction: function(){
    console.log("Test Function 1 Hit");
    this._testFunction2();
    var timer = null;
    timer = setInterval(function(){
        this._testFunction3();
    }, 1000);
},

Expected result:

Console logging following:

  • Test Function 1 Hit
  • Test Function 2 Hit
  • Test Function 3 Hit (every 1 second)

Actual result:

Console logging following:

  • Test Function 1 Hit
  • Test Function 2 Hit
  • Uncaught TypeError: this._testFunction3 is not a function

I have tried many things and just can't get it to work...

Greg
  • 503
  • 5
  • 30

1 Answers1

0

Does this help you?

function _testFunction3() {
    console.log("Test Function 3 Hit");
}
function _testFunction2() {
    console.log("Test Function 2 Hit");
}
function _testFunction() {
    console.log("Test Function 1 Hit");
    this._testFunction2();
    var timer = null;
    timer = setInterval(this._testFunction3,  1000);
}
_testFunction();

I think part of the problem was that defining functions like _function: function(){} must be invalid.

(Note: while debugging, I got a different error, "Function statements require a function name").

Lemondoge
  • 959
  • 4
  • 17