Refresh page when user is missing and there are no videos on page

Asked

Viewed 705 times

3

I need to refresh my website page when the user is inactive for 15 minutes and there are no embedded videos in it.

if the user is inactive on a page of the site for 15 minutes the page should be updated, but cannot update if the page has some embedded youtube video.

Whenever there is an embedded video it will use this code:

<iframe width="1280" height="720" src="https://www.youtube.com/embed/*VIDEOID*" frameborder="0" allowfullscreen></iframe>
  • We know if there is video in case there is any iframe on the page?

  • @Yes, whenever there is an iframe it will be a video, if that was the question :)

  • Yes, I put a way down, but it is with javascript/jquery, only with html/php can not do this.

1 answer

5


Only with php/html you can’t do this, but here’s an example of doing with javascript/jquery:

<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script>
var idleTime = 0;
$(document).ready(function () {
    //Increment the idle time counter every minute.
    if($('iframe').length < 1) // caso não exista iframe vamos incrementar o contador (idleTime) de 1 em 1 minuto
        var idleInterval = setInterval(timerIncrement, 60000); // 1 minutos

    // resetamos o contador caso sejam detetados estes eventos
    $(this).mousemove(function (e) {
        idleTime = 0;
    });
    $(this).keypress(function (e) {
        idleTime = 0;
    });
    $(window).on('scroll', function (e) {
        idleTime = 0;
    });
});

function timerIncrement() {
    idleTime = idleTime + 1;
    if (idleTime > 14) { // 15 minutes
        window.location.reload();
    }
}
</script>

Broken example from here

If there was no need to detect any activity on the page would suffice on head of your html:

<meta http-equiv="refresh" content="900">

Where content is the number of seconds before refresh

  • sorry I’m starting to work with this now, but I did the following http://i.imgur.com/8m6DJde.png and it doesn’t seem to update the page. I tried to modify to update every 10 seconds to see if it works, but nothing happens. I did something wrong?

  • I’m going to see if something’s wrong, I confess I didn’t test @talnun. It’s important that you’re importing jquery, but I’m going to try it fast

  • @talnun just tested, everything worked ok, changes to setInterval(timerIncrement, 100); to test, do not forget that we are working with milliseconds

  • Note working here @talnun: https://jsfiddle.net/eeuqq7ux/

  • really missed importing jquery, it worked as expected. Gratitude.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.