JavaScript getting 'unsafe-eval' with setInterval

1k views Asked by At

I get this error with the first code snippet below

Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "script-src 'self' 'unsafe-inline'".

    class SomeClass {
     constructor(){
           
        this.startTime = 0;
        this.gameTimer = setInterval(this.myTimer(), 1000);

    }

    myTimer() {
        this.startTime++;
        document.getElementById('time-count').innerHTML = this.startTime;

    }
}

But with a lambda function everything works fine:

    class SomeClass {
      constructor(){

         this.startTime = 0;
         this.gameTimer = setInterval(() => {
             this.startTime++;
             document.getElementById('time-count').innerHTML = this.startTime;
         }, 1000);
        
    }
}

I would like to know is there a way to declare a method or a function that I can set as an argument in the setInterval function (what I'm calling the constructor) without getting the 'unsafe-eval' - error ?

Ps. It would be a great plus to also know why I'm getting the error in the first snippet.

0

There are 0 answers