How can i make this JS Typing animation repeat in every Three second

Here is JS+ Babel Typing animation, Now it start on-page load, and we can erase and retype it from the button given, But i want to repeat (Retype) it in every Three second if the Checked, How can i do that.

i tried this plz check:

class TextAnimation {

    constructor(text, opt_domElement) {
        this.text = text;
        this.curChar = 0;
        this.inTimeout = 0;
        this.outTimeout = 0;
        this.domElement = opt_domElement || this.createDomElement();
    }
    
    createDomElement() {
        const elem = document.createElement('p');
        elem.className = 'blink';
        elem.innerHTML = ' ';
        document.body.appendChild(elem);
        return elem;
    }

    getDelay(char, isLastChar = false) {
        let delay = this.delay;
        switch(char) {
            case ' ':
                delay = 3;
                break;
            case ',':
                delay = 3;
                break;
            case '.':
            case '!':
            case '?':
                delay = 3;
                break
            default:
                delay = 15;
           
 
        }

        if (isLastChar) {
            delay = 0;
        }
        return delay;
    }
    
    type(callback) {
        this.curChar++;
        this.domElement.innerHTML = this.text.substr(0, this.curChar);
        if (this.curChar < this.text.length + 1) {
            const prevChar = this.text.substr(this.curChar - 1 , 1);
            this.inTimeout = setTimeout(() => this.type(callback), this.getDelay(prevChar, this.curChar === this.text.length));
        } else {
            this.curChar = this.text.length;
            callback();
            return;
        }
        // console.log('type', this.curChar);
    }
    
    erase(callback) {
        this.curChar--;
        this.domElement.innerHTML = this.text.substr(0, this.curChar);
        if (this.curChar >= 0) {
            this.outTimeout = setTimeout(() => this.erase(callback), 10);
        } else {
            this.curChar = 0;
            callback();
            return;
        }
        // console.log('erase', this.curChar);
    }
    
    stopCurrentAnimation() {
        clearTimeout(this.inTimeout);
        clearTimeout(this.outTimeout);
    }
    
    animateIn(callback = () => {}) {
        this.stopCurrentAnimation();
        this.type(callback);
    }
    
    animateOut(callback = () => {}) {
        this.stopCurrentAnimation();
        this.erase(callback);
    }
    
}

console.clear();

const type = document.getElementById('type');
const erase = document.getElementById('erase');

const question = document.getElementById('qqq').innerHTML;
const foo = new TextAnimation(question);
foo.animateIn(() => { console.log('autoplay done'); });

type.addEventListener('click', () => foo.animateIn(() => { console.log('animation in done'); }), false);
erase.addEventListener('click', () => foo.animateOut(() => { console.log('animation out done'); }), false);
  html,
body {
    width: 100%;
    height: 100%;
    padding: 20px;
    margin: 0;
}

body {
    background-color: #000;
}

p {
    display: inline-block;
    font-family: monospace;
    font-size: 11px;
    line-height: 11px;
    min-height: 11px;
    color: #fff;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    margin: 0;
}

#qqq {display: none}


.blink:after {
    content: '';
    position: absolute;
    top: 0;
    right: -10px;
    width: 5px;
    height: 100%;
    background-color: #fff;
    animation: blinker 0.8s linear infinite;
}

@keyframes blinker {
    50% {
        opacity: 0;
    }
}
<button id="type">
    type
</button>
<button id="erase">
    erase
</button> <b style="color: yellow;">Checkbox:</b> <input type="checkbox" id="myCheck">
<br>

<div id="qqq">
<h3>This is the Typing Content , i want to make it in every three second it should be retype from start if the check button is cheked / enabled</h3> 
<h2>Any HTML Content</h2>

</div>
    <script type="text/javascript" src="https://fiddle.jshell.net/js/babel/babel.js"></script>
        <script src="https://fiddle.jshell.net/js/stringify.js" charset="utf-8"></script>

i tried to making solve this by trying this codes, But i didn’t get success, i Hopw you master may be solve this. plz

37 thoughts on “How can i make this JS Typing animation repeat in every Three second”

Leave a Comment