简体   繁体   English

如何显示每10秒更改一次的横幅并尊重页面刷新的时间?

[英]How I can display a banner that changes every 10 seconds and respect the period while the page refreshes?

I am developing a builder of advertising campaigns on js and php that displays a banner every 10 seconds with setInterval function of javascript, but when the site refreshes the setInterval restarts. 我正在开发一个基于js和php的广告活动的构建器,该构建器使用javascript的setInterval函数每10秒显示一条横幅,但是当网站刷新时,setInterval会重新启动。 if you refresh the page in less than 10 seconds it will always display the same banner. 如果您在不到10秒的时间内刷新页面,它将始终显示相同的横幅。

Is there a way to maintain a setInterval function in the server side, or something? 有没有办法在服务器端维护setInterval函数,或者其他方法?

I'm trying this with javascript storing each second on a localStorage (HTML5) to continue in the same second when you refresh the page, but I think that is not the way to do it. 我正在尝试用javascript将每一秒存储在localStorage(HTML5)上,以在刷新页面时在同一秒内继续进行操作,但是我认为这不是做到这一点的方法。

I hope someone will help me to jump this hurdle that has breaking my head. 我希望有人能帮助我克服这个困扰我的障碍。 :) :)

You need to persist the state of the interval timer somewhere, either on the server or on the client. 您需要将间隔计时器的状态保存在服务器或客户端上的某个位置。 Local storage sounds like one option. 本地存储听起来像一个选择。 Other options would be a cookie, a url parameter, or a form parameter if you are using HTTP post. 如果您使用的是HTTP发布,则其他选项可能是cookie,url参数或form参数。

You can store your timer in a session which will only update every ten seconds 您可以将计时器存储在一个会话中,该会话仅每十秒钟更新一次

<?php
if (!isset($_SESSION)) {
  session_start();
}
$time = time();
echo $time.'<br>';
$interval = 10; // ten seconds
if(!isset($_SESSION['timer']) || $_SESSION['timer'] == '') { // if the timer has not been started
    $_SESSION['timer'] = $time; // sets the timer to start now
    echo 'start timer = '.$_SESSION['timer'].'<br>';
} else { // the timer has already been started
    echo 'timer = '.$_SESSION['timer'].'<br>';
    if(($_SESSION['timer'] + $interval) < $time) {
        // get new banner
        echo 'get new banner<br>';
        $_SESSION['timer'] = $time; // start the timer again
    } else {
        echo 'not yet';
    }
}
?>

Try running this code and refresh the page every second... you will see "get new banner" only every ten seconds. 尝试运行此代码并每秒刷新一次页面……您将仅每十秒钟看到一次“获取新横幅”。 Otherwise you will see "not yet". 否则,您将看到“尚未”。

You can put your code to get the new banner here `// get new banner' 您可以将代码放在此处以获取新横幅`//获取新横幅'

Let me know if this helps. 让我知道是否有帮助。

Happy Coding ! 编码愉快!

Is there a way to maintain a setInterval function in the server side, or something? 有没有办法在服务器端维护setInterval函数,或者其他方法?

You say you're using PHP, so just use a PHP session to record when the last banner was loaded. 您说您正在使用PHP,因此只需使用PHP会话来记录上一个横幅广告的加载时间。 If you get another call and the session time is <10 seconds ago then serve the same banner, otherwise serve a new one. 如果您接到另一个电话,并且会话时间小于10秒,则使用相同的横幅,否则使用新的横幅。

Yes, you'll still need a 10 second timer in JavaScript to trigger the banner to refresh, but you don't need all that stuff with localStorage -- it sounds like you're overthinking a really simple problem. 是的,您仍然需要JavaScript中的10秒计时器来触发横幅刷新,但是localStorage并不需要所有这些东西-听起来您似乎在考虑一个非常简单的问题。

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

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