I want the letters 'q' to be written only once per second. But it doubles every second. How to do this with this command?
function writeNow() {
document.write('q');
setInterval(writeNow, 1000);
}
writeNow();
You can use setTimeout
function writeNow() {
document.write('q');
setTimeout(writeNow, 1000);
}
writeNow();
Or setInterval
function writeNow() {
document.write('q');
}
setInterval(() => {
writeNow()
}, 1000);
You want to set the interval outside the function, when you call it inside the function, it will be recursive
Try
function writeNow() {
document.write('q');
}
setInterval(writeNow, 1000);
writeNow();
And I highly recommend against document.write
as it is deprecated
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With