简体   繁体   English

如何使用 Javascript 随机更改计时器上的 HTML 元素 - 跨越 web,而不仅仅是在浏览器中?

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

Sorry if my question is phrased dumbly - i didn't know exactly how to explain, Basically, I'm practicing creating a website with HTML, CSS.对不起,如果我的问题措辞很愚蠢 - 我不知道如何解释,基本上,我正在练习使用 HTML、CSS 创建一个网站。 and JavaScript, I have a picture element on my page.和 JavaScript,我的页面上有一个图片元素。 among other elements.在其他元素中。

What I want to achieve is randomly changing that 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.我写它的方式,它只在我的浏览器中运行随机图片 function,并在我每次刷新页面时重置。

I am 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.我目前正在使用 setInterval() 并不断检查时间以查看当前 date.getMinutes() == 0,然后我认为这是一个新小时,我调用 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 this.我不想要这个。 I want consistency across time and space.我想要跨越时间和空间的一致性。 Haha.哈哈。

I am not asking for specific coding instructions - I am more trying to understand the concept.我不是在要求具体的编码说明——我更想理解这个概念。 How does one change elements internet-wide?如何在互联网范围内改变元素?

Hopefully I am making this clear.希望我说清楚了。 Thank you!!谢谢!!

This would be one way of doing it.这将是一种方法。 Using getTime() to retrieve the UTC time in seconds, convert to hours and then use the last digit for a world time reference to shift the image.使用getTime()以秒为单位检索 UTC 时间,转换为小时,然后使用最后一位作为世界时间参考来移动图像。

If you would like to shift between 99 images then use the last two digits of the hour variable.如果您想在 99 张图像之间切换,请使用hour变量的最后两位。 And if you would like to shift between more images then you understand the logic...如果你想在更多图像之间切换,那么你就会理解逻辑......

 const d = new Date(), hours = d.getTime()/(1000*60*60), n = Math.round(hours/100000), imgs = [ 'https://picsum.photos/id/237/200/300', 'https://picsum.photos/id/237/200/300', 'https://picsum.photos/id/237/200/300', 'https://picsum.photos/id/237/200/300', 'https://picsum.photos/id/237/200/300', 'https://picsum.photos/id/237/200/300', 'https://picsum.photos/id/237/200/300', 'https://picsum.photos/id/237/200/300', 'https://picsum.photos/id/237/200/300', 'https://picsum.photos/id/237/200/300' ]; document.getElementById('root').innerHTML = '<img src=' + imgs[n] + '/>';
 <div id="root"/>

Here is a slightly different pseudo random number generator based on a seed value calculated from the current UTC hour:这是一个略有不同的伪随机数生成器,它基于从当前 UTC 小时计算的种子值:

 const d = new Date(), imgs = [...Array(100)].map((c, i) => 'https://picsum.photos/id/X/200/300'.replace("X", 216 + i)); // define a seed-based random number generator (later: "rand()"), as suggested in // https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript function sfc32(a, b, c, d) { return function() { var t = (a + b) | 0; a = b ^ b >>> 9; b = c + (c << 3) | 0; c = (c << 21 | c >>> 11); d = d + 1 | 0; t = t + d | 0; c = c + t | 0; return (t >>> 0) / 4294967296; } } // create a seed, based on the current hour in UTC time: const seed = Math.round(new Date().getTime() / (1000 * 60 * 60)); // initialise the number generator rand() with this seed: const rand = sfc32(0x9E3779B9, 0x243F6A88, 0xB7E15162, seed ^ 0xDEADBEEF); // carry out some initial "mixing"... for (let n = 15; n--;) rand(); // now rand() will deliver a pseudo-random number that will be reproducible for identical seeds. let src = `<img src="${imgs[Math.floor(rand()*100)]}">`; document.getElementById("root").innerHTML = `<img src="${imgs[Math.floor(rand()*imgs.length)]}">`;
 <div id="root"></div>

In the expression [...Array(100)] I limited the number of pictures to choose from to 100. You can of course use any number of pictures here.在表达式[...Array(100)]中,我将可供选择的图片数量限制为 100。您当然可以在这里使用任意数量的图片。

You will have to do this in the frontend since you don't know when a user will load your page.您必须在前端执行此操作,因为您不知道用户何时会加载您的页面。

Have an array of images, say 24, one for each hour of a day.有一组图像,比如 24 张,一天中的每个小时一张。

When a page loads you get the current time in some fixed point - let's say you use UTC, you can't use local time for this.当页面加载时,您会在某个固定点获得当前时间 - 假设您使用 UTC,您不能为此使用本地时间。 Once you have this standard time, work out the last hour that has been passed.一旦你有了这个标准时间,计算过去的最后一个小时。 Use this to look up which image to show.使用它来查找要显示的图像。

Also use this to setTimeout of 60minutes - currentminutes.也可以使用它来设置 60 分钟的超时时间 - 当前分钟。

On the timeout, move to the next image and set another timeout of 60minutes - current minutes.在超时时,移动到下一个图像并设置另一个 60 分钟的超时 - 当前分钟。 And so it goes on.所以它继续。

That way any user wherever in the world will see the same background image, give or take a bit of timing inaccuracy (which depends for example on what else their system is doing at the time).这样,世界上任何地方的任何用户都将看到相同的背景图像,给出或接受一些时间不准确性(这取决于例如他们的系统当时正在做什么)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM