Created
December 17, 2020 16:49
-
-
Save danvega/e51c567c63a2a8398b0d2b6b165d62ea to your computer and use it in GitHub Desktop.
SetTimeout Vue Delay
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<h1>Random Dad Jokes</h1> | |
<p v-if="loading">Loading...</p> | |
<p v-else>{{ joke }}</p> | |
</template> | |
<script> | |
export default { | |
name: "App", | |
data() { | |
return { | |
joke: "", | |
loading: true, | |
}; | |
}, | |
methods: { | |
loadJoke() { | |
fetch("https://icanhazdadjoke.com/", { | |
headers: { | |
Accept: "application/json", | |
}, | |
}) | |
.then((response) => { | |
return response.json(); | |
}) | |
.then((data) => { | |
if (data.status === 200) { | |
this.joke = data.joke; | |
this.loading = false; | |
} | |
}) | |
.catch((err) => console.log(err)); | |
}, | |
}, | |
mounted() { | |
setTimeout(this.loadJoke, 3000); | |
}, | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment