Jquery refresh page after 5 seconds | Javascript

In this tutorial, We will see the Jquery refresh page after 5 seconds. To refresh the page in jquery by using location.reload method, Now if you want to refresh it after 5 seconds you have to use the setTimeout function. 


Jquery refresh page after 5 seconds | Javascript


In the below example 5000 means 5 seconds 😃

1. Jquery refresh page after 5 seconds 



  setTimeout(function () {

  window.location.reload();

  }, 5000);


2. Jquery refresh page after ajax success with 5 seconds


           $.ajax({

                type: 'Post',

                contentType: 'application/json;charset=utf-8',

                url: '~/your/url',

                data: JSON.stringify({ data: getdata }),

                success: function (response)

                {

                    var Res = (response.d);

                    if (Res !== '') {

                        setTimeout(function () {

                            window.location.reload();

                        }, 5000);

                    }

                }

            });

3. Jquery refresh page after ajax success without 5 seconds


Remove the  setTimeout(function () 

           $.ajax({

                type: 'Post',

                contentType: 'application/json;charset=utf-8',

                url: '~/your/url',

                data: JSON.stringify({ data: getdata }),

                success: function (response)

                {

                    var Res = (response.d);

                    if (Res !== '') {                      

                            window.location.reload();                       

                    }

                }

            });

4. Perform page refresh after some time on the button click


    <!DOCTYPE html>

<html>

<head>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

    <script>

           $(".refresh").on("click", function (e) {

             setTimeout(function(){

             location.reload();

             }, 5000)

            });

 </script>

</head>

<body>

    <button class="refresh">Refresh After some times</button>

 </body>

</html>


Also Read

Jquery string to number 

For more tips and tricks, stay tuned with us. 😉

Post a Comment

Post a Comment (0)

Previous Post Next Post