• iconJakarta, Indonesia
  • iconlaratech.channel@gmail.com

+62081290348080

icon

Need Help?

+62081290348080

How to refresh a page if no activity after 1 minute using jQuery

How to refresh a page if no activity after 1 minute using  jQuery

How to refresh a page if no activity after 1 minute using jQuery

Hello, in this article I will show you how to refresh your page if there is no activity from the user's browser. This is a very easy step. On other hand, we can say If you want to refresh whereby there is no activity then you need to figure out how to define activity.

 

Let's say we refresh the page every minute unless someone presses a key or moves the mouse.

 

This approach uses jQuery for event binding:

 

Below is the code to use:

 

<script>
    var time = new Date().getTime();

    $(document.body).bind("mousemove keypress", function(e) {

       time = new Date().getTime();

    });

    function refresh() {
    
     if(new Date().getTime() - time >= 60000) 
        window.location.reload(true);

    else

        setTimeout(refresh, 10000);

     }

    setTimeout(refresh, 10000);

</script>

Thank you for reading this article, if you have anything that you wanted to ask leave your comment and we discuss it. I hope you understand a How to refresh a page if no activity after 1 minute using jQuery by now.
{"id":1,"name":"Laratech","username":"admin","avatar":"users\\June2022\\WSYjSsq966HbiJIgJyhx.png"}

About the author:

Laratech is a fullstack developer based in Indonesia.
You can find more about him at Twitter

0 Comments

Leave a comment

Tags