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

125
Views
the problem with writing a script for animated text output

I can't understand what the problem is, I'm writing a script that will have to output the text on its own, as if someone is typing, but I'm constantly throwing out the same error, do not tell me what can be done in this case, I will be grateful.

the error is that the substring method does not work Uncaught TypeError: Cannot read properties of undefined (reading 'substring')

let quoteArray = [];
let index = 0;
let textPosition = 0;
let flag = true;
let destination = document.querySelector('#typedtext');

window.addEventListener('load', typewriter);

function loadQuote() {
const url = "https://api.quotable.io/random";

fetch(url)

.then(response => {
    if(response.ok) {
        return response.json();
    } else {
        console.log(response.status);
    }
})

.then(data => {
    quoteArray[index] = data.content;
});

}

function typewriter(){
if(flag) {
    loadQuote();
    quoteArray[index] + ' ';
    flag = false;
}

destination.innerHTML = quoteArray[index].substring(0, textPosition); the error is here

if(textPosition++ != quoteArray[index].length){
    setTimeout('typewriter()', 100);
} else {
    quoteArray[index] = ' ';
    setTimeout('typewriter()', 3000);
    textPosition = 0;
    flag = true;
}

}

about 4 years ago · Juan Pablo Isaza
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!