How to display random text at random time automatically

rap8557

Banned - Selling via PM
Joined
Sep 15, 2008
Messages
2,000
Reaction score
631
So im trying to add a random pop up text automatically after random time without reloading a page,

Code:
<p class="custom-notification-content">
            Someone from
   <script>var Country = ['Edmonton, Canada', 'California, United States', 'New Jersey, United States','Birmingham, United Kingdom', 'Melbourne, Australia', 'Paris, France'];
var randCountry = Country[Math.floor(Math.random() * Country.length)];
document.write(randCountry);</script>

             <br>Purchased <b>

            <script>var randNumber = Math.floor(Math.random() * 9) + 1; document.write(randNumber);</script><script>var Zeros = ['0', '00', '000'];
var randZeros = Zeros[Math.floor(Math.random() * Zeros.length)];
document.write(randZeros);</script></b>

<script>var Product = ['apple','grapes','comb','hat'];
var randProduct = Product[Math.floor(Math.random() * Product.length)];
document.write(randProduct);</script>   

            <small>(<script>var randNumber = Math.floor(Math.random() * 5) + 2; document.write(randNumber);</script> hours ago)</small>
          </p>

The var Product can be added a lot of product or removed, i tried this code but it didn't work.
The script is only getting 1 country 1 number and 1 product, and not changing automatically. You need to reload the page in order to change.

Please help
Thanks
 

MrFufu

Newbie
Joined
May 31, 2017
Messages
16
Reaction score
5
Hey,

I hope this code can help you:

HTML:
    <p class="custom-notification-content">
      Someone from <span id="country"></span><br />
      Purchased <span id="product"></span> (<span id="hours"></span> hours ago)
    </p>

    <script>
      // Helper functions
      var randomNumberBetween = (min, max) =>
        min + Math.floor(Math.random() * max);
      var pickRandomItemFromArray = array =>
        array[randomNumberBetween(0, array.length - 1)];

      // Datas
      var timeBetweenUpdates = 2000;
      var countries = [
        "Edmonton, Canada",
        "California, United States",
        "New Jersey, United States",
        "Birmingham, United Kingdom",
        "Melbourne, Australia",
        "Paris, France"
      ];
      var products = ["apple", "grapes", "comb", "hat"];

      // Dynamic elements
      var countryTextElement = document.querySelector("#country");
      var productTextElement = document.querySelector("#product");
      var hoursTextElement = document.querySelector("#hours");

      // Update texts function
      var updateTexts = () => {
        countryTextElement.innerText = pickRandomItemFromArray(countries);
        productTextElement.innerText = pickRandomItemFromArray(products);
        hoursTextElement.innerText = randomNumberBetween(1, 48);
      };

      // First update
      updateTexts();

      // Interval updates
      setInterval(() => {
        updateTexts();
      }, timeBetweenUpdates);
    </script>

It's far from being perfect but it may give you some ideas
 
Last edited:

MrFufu

Newbie
Joined
May 31, 2017
Messages
16
Reaction score
5
If you want to see how this code is running you can go to codesandbox [dot] io add /s/0qlpk04nyv to the URL
 

fb3003

Jr. VIP
Jr. VIP
Joined
Aug 14, 2015
Messages
4,454
Reaction score
3,018
Website
bit.ly
if you think you will have better ranking for your page by having it change a random text at random time, then don't waste your time. i have done multiple tests using different wordpress plugins, it does not do jack shit for google ranking improvement.
 

vocotnhan

Jr. VIP
Jr. VIP
Joined
Nov 30, 2012
Messages
100
Reaction score
72
if you think you will have better ranking for your page by having it change a random text at random time, then don't waste your time. i have done multiple tests using different wordpress plugins, it does not do jack shit for google ranking improvement.
I think he try to do fake user purchased for dropshipping
 

rap8557

Banned - Selling via PM
Joined
Sep 15, 2008
Messages
2,000
Reaction score
631
Hey,

I hope this code can help you:

HTML:
    <p class="custom-notification-content">
      Someone from <span id="country"></span><br />
      Purchased <span id="product"></span> (<span id="hours"></span> hours ago)
    </p>

    <script>
      // Helper functions
      var randomNumberBetween = (min, max) =>
        min + Math.floor(Math.random() * max);
      var pickRandomItemFromArray = array =>
        array[randomNumberBetween(0, array.length - 1)];

      // Datas
      var timeBetweenUpdates = 2000;
      var countries = [
        "Edmonton, Canada",
        "California, United States",
        "New Jersey, United States",
        "Birmingham, United Kingdom",
        "Melbourne, Australia",
        "Paris, France"
      ];
      var products = ["apple", "grapes", "comb", "hat"];

      // Dynamic elements
      var countryTextElement = document.querySelector("#country");
      var productTextElement = document.querySelector("#product");
      var hoursTextElement = document.querySelector("#hours");

      // Update texts function
      var updateTexts = () => {
        countryTextElement.innerText = pickRandomItemFromArray(countries);
        productTextElement.innerText = pickRandomItemFromArray(products);
        hoursTextElement.innerText = randomNumberBetween(1, 48);
      };

      // First update
      updateTexts();

      // Interval updates
      setInterval(() => {
        updateTexts();
      }, timeBetweenUpdates);
    </script>

It's far from being perfect but it may give you some ideas
It works great! will just to adjust the timer. Thank you very much!!! :)
 

rap8557

Banned - Selling via PM
Joined
Sep 15, 2008
Messages
2,000
Reaction score
631
I think he try to do fake user purchased for dropshipping
Yes so that new users will think that the website is very active with a lots of clients, i've saw this on one of the eCommerce website. Maybe this will work to gain trust to new clients even if the website is new. :)
 
Top