I have created a random quote generator for my Angular app. The component code looks like this:
qotd = this.quotes[Math.floor(Math.random() * this.quotes.length)];
That's pulling from data that looks like this:
quotes = [
{
quote: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus euismod magna magna, euismod tincidunt libero dignis.",
author: 'Sorato Violasa'
},
{
quote: "Nullam dignissim accumsan magna vitae rhoncus. Phasellus euismod magna magna, euismod tincidunt libero dignis.",
author: 'Vito Dignaora'
},
{
quote: "In tincidunt imperdiet augue, quis sollicitudin mi tincidunt ut.",
author: 'Hivalo Amettioa'
},
{
quote: "hasellus accumsan erat vitae enim blandit, quis euismod ipsum sollicitudin.",
author: 'Grasha Plojiva'
},
];
And then in my view I do this:
<div class="quotes">
<p class="quote">
<br><br>
~
</p>
</div>
The thing is, right now this will generate a new quote every time the component re-loads, which can be multiple times within a single session. What I realize now would be better is to make this a daily quote generator. So it would only generate a new quote if the date changes. What's the simplest way to implement something like this? It's easy enough to generate the date and day of the week, like this:
date = new Date();
dayNumber = this.date.getDay();
But how would I compute when the day of the week changes in order to fire off a new instance?
Aucun commentaire:
Enregistrer un commentaire