How do I make a video play at a random second every time the page is refreshed

Question:
How do I make a video play at a random second every time the page is refreshed

Product:
Wix Studio Editor

What are you trying to achieve:
I have a banner film playing on my homepage. I want it to start at any random second when you refresh the page. At the moment it plays in loop at starts at the beginning every time, I want it to always be random.

What have you already tried:
I’ve tried ChatGPT recommended Jave code

Additional information:
I’m basically trying to stop the same first 5 secs always being the same every time you land on the page. It becomes very repetitive.

1 Like

Try this one…

$w.onReady(function () {
  const video = $w("#videoPlayer1"); console.log(video);
  const duration = video.duration; console.log('Duration: ', duration);
  if (duration && duration > 0) {
    const randomSecond = Math.floor(Math.random() * duration); console.log('Random-Seconds: ', randomSecond)
    video.seek(randomSecond);
    video.play();
  } else {console.log('No VIDEO loaded!!!');}
})
  1. Paste this code onto your page, where your videoPlayer is located at.
  2. To be able to use codings → first activate the DEV-MODE in your Wix-Editor.
  3. Publish and open the published site.
  4. Give me a feedback and like it :upside_down_face:

HOLY-MOLY!!! → Did not work right? :upside_down_face:

Well this is called → CYBER-SECURITY ←
In modern webbrowsers you can’t do → REAL AUTOPLAY ← anymore, it is restricted.

But since CODE-NINJA is a real NINJA :upside_down_face:, let’s do a tricky WORK-AROUND :wink:

Now remove the old one and paste this one…

$w.onReady(()=> {
	const video = $w("#videoPlayer1"); console.log(video); video.mute();
	const duration = video.duration; console.log('Duration: ', duration);
	if (duration && duration > 0) {console.log('GO');
		const randomSecond = Math.floor(Math.random() * duration); console.log('Random-Seconds: ', randomSecond)
		video.seek(randomSecond);
		video.play();
	} else {console.log('No VIDEO loaded!!!');}
})

And again → HOLY-Sh…!!! Working?
What was the trick??? :grin: