malarkey
v2.0.2
Published
Simulate a typewriter effect in vanilla JavaScript.
Maintainers
Readme
malarkey

Simulate a typewriter effect in vanilla JavaScript.
- Flexible API allowing granular control
- Option to repeat the sequence indefinitely
- Allows stopping and resuming the sequence on-the-fly
- 524 bytes gzipped
Usage
<div class="typewriter"></div>const malarkey = require('malarkey')
const element = document.querySelector('.typewriter')
function callback (text) {
element.textContent = text
}
const options = {
typeSpeed: 50,
deleteSpeed: 50,
pauseDuration: 2000,
repeat: true
}
malarkey(callback, options)
.type('Say hello')
.pause()
.delete()
.type('Wave goodbye')
.pause()
.delete()API
const malarkey = require('malarkey')const m = malarkey(callback [, options])
Initialise the typewriter effect with the given optional options settings.
callbackis a function that is invoked for everytypeanddeleteevent in the sequence. The function signature is(text), wheretextis the string of characters that have been typed so far.optionsis an object literal:Key | Description | Default :--|:--|:--
typeSpeed| Duration in milliseconds totypea single character |50deleteSpeed| Duration in milliseconds todeletea single character |50pauseDuration| Duration in milliseconds topause|2000repeat| Whether to repeat the entire sequence indefinitely |false
m.type(string [, options])
Type the given string, one character at a time.
- Set
options.speedto override the defaulttypeSpeed.
m.delete([characterCount, options])
Delete the specified number of characters, one character at a time.
characterCountis a positive integer. If not specified, all characters previously typed will be deleted, one character at a time.- Set
options.speedto override the defaultdeleteSpeed.
m.pause([options])
Do nothing for some duration.
- Set
options.durationto override the defaultpauseDuration.
m.clear()
Immediately clear all characters that were typed.
m.call(fn)
Call the given fn function.
- The function signature of
fnis(callback, text). Invokecallbackto signal thatfnhas finished execution and that we can move on to the next event in the sequence.
m.triggerStop([fn])
Stops the sequence. Calls the given fn function when the sequence has been stopped.
- The function signature of
fnis(text).
m.triggerResume()
Resume the sequence. Has no effect if the sequence is already running.
m.isStopped()
Returns true if the sequence is currently stopped, else returns false.
Installation
Install via yarn:
$ yarn add malarkeyOr npm:
$ npm install --save malarkey