Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

271
Views
Why I see unsafe-eval alert when using setTimeout with strings?

Here is the example how it appear in Chrome dev tools:

setTimeout(console.log(`String`), 100);

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

What does this alert means and what is the security concern of using setTimeout with strings?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You're using setTimeout wrong.

When you append parens after a function in JS, it immediately calls the function, and "replaces" the expression with the return value of that function.

But, with setTimeout, you don't want to call that function, you want setTimeout to call it! To do that, you give the function to setTimeout without calling it, and setTimeout will call it internally for you when the timer goes off.

But your code does call the function, console.log, and only gives its return value (undefined) to setTimeout. setTimeout doesn't throw an error though - it has an old "feature" that if you pass a string to it, it evaluates it as if you used eval.

undefined is not a string, but neither is it a function, so when the timer expires, setTimeout will cast it to a string ("undefined") and try to eval it. That'why you get the warning.


You can fix this by passing your function to setTimeout "the good way": wrap your call into another function. This way, your outer function will call console.log when it is called, so you can pass that to setTimeout:

setTimeout(() => console.log(`String`), 100);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!