使JavaScript随机报价生成器每天只生成一个报价

时间:2017-10-12 22:28:18

标签: javascript angular date typescript random

我为Angular应用程序创建了一个随机引用生成器。组件代码如下所示:

qotd = this.quotes[Math.floor(Math.random() * this.quotes.length)];

这是从这样的数据中提取出来的:

  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'
    },
  ];

然后在我看来我这样做:

<div class="quotes">
    <p class="quote">
        {{qotd.quote}} <br><br>
        ~ {{qotd.author}}
    </p>
</div>

问题是,现在每次重新加载组件时都会生成一个新的引号,在单个会话中可以多次。我现在意识到更好的是将它作为日常报价生成器。因此,如果日期发生变化,它只会生成新的报价。实现这样的事情最简单的方法是什么?生成一周中的日期和星期很容易,如下所示:

  date = new Date();
  dayNumber = this.date.getDay();

但是,为了触发新实例,我如何计算星期几的变化?

3 个答案:

答案 0 :(得分:1)

考虑一个服务器准备3个引号的方案:一个用于昨天,今天和明天。每个新的一天,昨天被删除,并添加了新的明天。这样,客户端的报价可以在客户当地时间午夜之后更改,并且每个人在同一个当地时间看到相同的报价。

地球上任何地方与UTC不同的时间超过14小时,因此如果服务器基于UTC的密钥,每个客户端都会有适当数量的报价,并且每次客户端都不会将整个报价数据库推送给每个客户端变化。

&#13;
&#13;
$update_result = $users->update_one(
                                    { 'userid' => $doc->{'userid'}},
                                    { '$set'     => { 'emails.[0].active' => false }},
                                    { 'upsert'   => 1}
                                );
&#13;
&#13;
&#13;

答案 1 :(得分:0)

不...等等... 如果您需要为每一天设置特定的报价,那是从报价列表中获取随机报价的不同场景,但在 eacb 24 hours, one day 上具有相同的报价。 如果您有一个数组,您可能会使用 rand 函数和一个计数来获取数组的值总数。 这将在每次加载时返回一个值。 因此,还有另一个名为 srand 的函数。 如果您使用 srand,您将设置一个种子 .. 并且该种子将成为 srand 的种子。

好的/

因此,种子设置 srand 结果......而 srand 进行随机化。 您也可以尝试 shuffle 功能。它也使用种子。

现在,您需要找到一个值用作种子,并且您需要该数字在 24 小时内发生变化。

要使用区间,需要一个参考值。可以使用服务器时间和用户代理时间。

如果您使用种子“42”,则 srand 返回的结果相同。

检查一下: 假设您有一个 42 个引号列表。 首先创建数组,设置种子变量,获取值。

$numbers = range(1,42); $seed = floor(time()/86400); 种子的值由服务器 unix 时间戳设置,其时区独立 (=UTC)。 根据需要间隔一天,使用 86400 值计算,一天的总秒数。 随机化数组 洗牌($数字); 并回显第一个结果。 让我们使用 200 个引号列表:

$quoteslist= range(1,200);
$seed = floor(time()/86400);
echo $seed;

$resultado = $quoteslist;
$seed = floor(time()/86400);
 shuffle($resultado);
echo $resultado[$i];

time() 返回的值,时间戳,更改为 18846,即今天的种子。

然后,使用该值作为种子打乱列表。

得到第一个结果。

// you need find the array count. Lets create an array of a 200 quotes .
$quotes = range(1,200);
$seed = floor(time()/86400);
echo 'the seed is:'. $seed.' and that changes on next day .';
echo '<hr>';

// makes another range to use the same index
$quotes = range(1,200); 
$resultado = $quotes;

// first value of the array, no randomizing (its 1)
echo 'the first result of the range array : ';
echo $resultado[0];
echo '<hr>';

// lets echo the result of the shuffle, not using the srand and the seed, it prints each time one value .
$quotes = range(1,200); 
$resultado = $quotes;
shuffle($resultado);
echo 'first result of thhe array, shuffled.. but not using the seed:  ';
echo $resultado[0];
echo '<br> this changes each refresh';
echo " <hr> ";


// this prints a random number for each day, basing on using the seed 

$quotes = range(1,200);
$resultado = $quotes;
echo 'today: ';
$seed = floor(time()/86400);
srand($seed);
shuffle($resultado);
echo $resultado[0];
echo " * this changes everyday, same result for all reloads  <hr> ";

echo '<br>';
// this is the seed of today. using
$quotes = range(1,200);
$resultado = $quotes;
echo 'the day of seed 18846: first day - result never changes, because used specific seed : ';
srand(18846);
shuffle($resultado);
echo $resultado[0];
echo " <hr> ";

echo 'the result for 18847 seed , next day - never changes : ';
$quotes = range(1,200);
$resultado = $quotes;
// this is 2 days after
srand(18847);
shuffle($resultado);
echo $resultado[0];

答案 2 :(得分:-1)

您可以采取的一种方法是通过localStorage将当天保存在客户端上。在您的组件中,检查我们的localStorage中是否有currentDay密钥,如果没有创建它,请保存对象内的currentDay值和qotd

下次重新加载组件时,您应该已经在localStorage中分配了一个qotd,所以我们要做的是检查localstorage中保存的日期以及我们生成的当前日期。如果他们不同,我们知道这一天已经改变了。

  //PSUEDOCODE (haven't tested, but should give you a good idea)
  function(){
    this.qotd = this.quotes[Math.floor(Math.random() * this.quotes.length)];

    if(!window.localStorage.getItem('todayDate')){    
        let currentDay = new Date().getDay();
        let storageItem = JSON.stringify({
          currentDay,
          qotd: this.qotd
        })
        window.localStorage.setItem('currentDay',storageItem)
      } 
      else {
        var existingQOTD = JSON.parse(window.localStorage.getItem('todayDate'));
        let currentDay = new Date().getDay();
        if(existingQOTD.currentDay !== currentDay){
        let storageItem = JSON.stringify({
          currentDay,
          qotd: this.qotd
        })
          window.localStorage.setItem('currentDay', storageItem)
        }

      }
  }
相关问题