Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypewriterJS scoping

There seems to be some kind of issue with Typewriter JS scoping. For instance, in the following code, deleteAll() executes no problem

const typewriter = new Typewriter('#typewriter');

typewriter.start();
typewriter.typeString('Hello, world');
typewriter.deleteAll();
<script src="https://unpkg.com/typewriter-effect@latest/dist/core.js"></script>
<div id="typewriter"></div>

however, when deleteAll() is put inside a function, it does not work.

const typewriter = new Typewriter('#typewriter');

typewriter.start();
typewriter.typeString('Hello, world');

const yes = document.querySelector("#yes");
yes.addEventListener('click', function() {
  typewriter.deleteAll();
});
<script src="https://unpkg.com/typewriter-effect@latest/dist/core.js"></script>
<div id="typewriter"></div>

<button id="yes">yes</button>

All the code is called after DOM content is loaded. Does anyone know what the issue is or how I can fix it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There is some weird bug in the typewriter code. You might want to report it that it seems to not restart after it is done.

Seems like it you call stop, with a pause, and start it will allow it to run.

const typewriter = new Typewriter('#typewriter');

typewriter.start();
typewriter.typeString('Hello, world');

const yes = document.querySelector("#yes");
yes.addEventListener('click', function() {
  typewriter.stop().pauseFor(1).start().deleteAll();
});
<script src="https://unpkg.com/typewriter-effect@latest/dist/core.js"></script>
<div id="typewriter"></div>

<button id="yes">yes</button>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading