Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I randomly change an HTML element on a timer using Javascript - across the web, not just within the browser?

I'm creating a website with HTML, CSS, and JavaScript. I have a picture element on my page, among other elements.

What I want to achieve is to randomnly change the picture every hour. I am able to do this perfectly fine by running it in my browser.

My question is: how does one randomly change an element across the board (the entire internet) for all users?

The way I have it written, it only runs the random pic function within my browser, and resets every time I refresh the page.

I'm currently using setInterval() and checking the time constantly to see if the current date.getMinutes() === 0, then I figure it's a new hour, and i call a changePicHourly() function.

I have no problem getting this to work. However, obviously every visitor to my page will see a different picture. I do not want that. I want consistency across time and space! Haha.

I'm not asking for specific coding instructions, I'm more focused on trying to understand the concept. How does one change elements internet-wide?

Hopefully I am making this clear. Thank you!!

like image 902
MissAri Avatar asked Jan 25 '26 08:01

MissAri


1 Answers

This would be one way of doing it without a back-end:

Using new Date.getTime() to retrieve the UTC time which is the same for all computers connected to the internet. Then convert this milliseconds to hours and create a pseudo-random number to shift the image every hour.

In the following code I'm using only ten images but the random number is meant to be used for up to 100 images.

const 
  hours = Math.round(new Date().getTime()/(1000*60*60)),
  numbers = [
    Number(hours.toString().slice(0,2)),
    Number(hours.toString().slice(2,4)),
    Number(hours.toString().slice(4,6))
  ],
  images = [
    'https://picsum.photos/id/237/200/300', 
    'https://picsum.photos/id/236/200/300', 
    'https://picsum.photos/id/235/200/300',
    'https://picsum.photos/id/234/200/300',
    'https://picsum.photos/id/233/200/300',
    'https://picsum.photos/id/232/200/300',
    'https://picsum.photos/id/231/200/300',
    'https://picsum.photos/id/230/200/300',
    'https://picsum.photos/id/229/200/300',
    'https://picsum.photos/id/228/200/300'
  ];

function hourlyRand(){
  const pseudoRandString = (numbersArr) => {
    let rand = 1
    numbersArr.forEach((n, i) => {
      if(n !== 0) rand = rand * n 
    })
    return rand.toString()
  }  
  
  // get a pseudo random number and convert to string
  let randString = pseudoRandString(numbers)

  // length of the pseudo random number string
  let L = randString.length

  // slice the string and convert to number
  let n = Number(randString.slice(L-3,L-1))
  
  // return the double digit hourly random number
  return n
}

// pseudo-random number between 0 and 99 
console.log('Hourly pseudo-random number: ' + hourlyRand())

// pseudo-random number between 0 and 9 
let n = Math.round(hourlyRand()/10)

document.getElementById('root').innerHTML = '<img src=' + images[n] + '/>'
<div id="root"/>

If it were my project I would build the logic in the back-end and then use an API in the front-end to render the images in whatever way I wish. This would allow all users to see the same changes occurring at a certain point in time.

like image 156
Gass Avatar answered Jan 26 '26 22:01

Gass



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!