T
T
Tosha13372022-01-08 11:26:15
JavaScript
Tosha1337, 2022-01-08 11:26:15

Why does the script not see the function in setInterval?

Hello!
Tell me why if you just type this code into the console, it works, and the button is created as part of the extension, but when pressed, it displays:

1 Uncaught ReferenceError: functionName is not defined

function functionName() {
alert('Имя') 
}

setInterval(() => {
document.querySelector('.button').innerHTML = '<button onclick="functionName()" type="button" >Показывать имя</button>' 
}, 1000)


Hope I phrased the question well.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GrayHorse, 2022-01-08
@Tosha1337

The function functionNameexists in the context of the script content.
When adding a button with a click on it, it will be called in the context of a web page where this function does not exist. 1. Add a button. 2. Add an event listener through it. Will work. onclick="functionName()"functionName

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question