Skip to content

Instantly share code, notes, and snippets.

@leemartin
Last active May 16, 2020 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leemartin/cbbae8eda29dd47a65aceb047bf9c935 to your computer and use it in GitHub Desktop.
Save leemartin/cbbae8eda29dd47a65aceb047bf9c935 to your computer and use it in GitHub Desktop.
The "Hitchhiker Typer" component used on Shelter In Space
<template>
<p><span class="start">{{ start }}</span><span class="end">{{ end }}</span></p>
</template>
<script>
export default{
props: {
phrase: {
type: String,
required: true
}
},
data() {
return {
index: 0
}
},
computed: {
typed() {
return phrase.substring(0, this.index)
},
start() {
return this.typed.substring(0, this.typed.length - 1)
},
end() {
return this.typed[this.typed.length - 1]
}
},
methods: {
startTyping() {
this.typing = setInterval(() => {
this.index += 1
if (this.index > this.phrase.length) {
this.stopTyping()
}
}, 50)
},
stopTyping() {
clearInterval(this.typing)
}
},
mounted() {
this.startTyping()
},
beforeDestroy() {
this.stopTyping()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment