Skip to content

Instantly share code, notes, and snippets.

@pejalo
Created August 10, 2023 23:07
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 pejalo/64cdf74f143c66e3d9810546777167ee to your computer and use it in GitHub Desktop.
Save pejalo/64cdf74f143c66e3d9810546777167ee to your computer and use it in GitHub Desktop.
Simple loading component showing " ..", ". .", ".. ", repeated every second
<script>
// Note: This is a waste of JavaScipt resources, and will not update nicely
// if you are running heavy computations. Better to use an SVG or CSS.
import { onMount } from "svelte"
const fullString = "..."
let currentString = ""
onMount(() => {
let i = 0
const numberOfSteps = fullString.length
const interval = setInterval(() => {
i = (i + 1) % numberOfSteps
currentString = fullString.substring(0, i) + "&nbsp;" + fullString.substring(i + 1)
}, 1000/numberOfSteps)
return () => {
clearInterval(interval)
}
})
</script>
<span class="loading">{@html currentString}</span>
<style>
.loading {
font-family: monospace;
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment