Home > Software design >  Jquery wait 30 seconds to scan after first result
Jquery wait 30 seconds to scan after first result

Time:06-03

Currently I have the below code, which works perfectly for what it is, however during the inital page load, it takes the 40 seconds before the content from handle.php appears on the page. How can I make the contents from handle.php appear on the page during inital load.

Then require 40 seconds for future scans?

$(document).ready(function(){

    jQuery(window).on("load", function(){
 
     refreshInterval = setInterval(function(){
        $('#box').load('handle.php');
     },40000);
 
    });
})

CodePudding user response:

Here is the exact code that you need.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>

<body>

    <h1>Stack overflow answer</h1>

    <div id="box"></div>

    <script>
    $(document).ready(function() {

        const userAlreadyVisited = localStorage.getItem('visited');

        // After the first load it will load the content from handle.php after 2 second every time
        if (userAlreadyVisited) {
            jQuery(window).on("load", function() {
                setTimeout(() => {
                    $('#box').load('handle.php');
                }, 5000);
            });
        } else {
            // The content of handle.php will be loaded immediately after the page load
            localStorage.setItem('visited', true);
            jQuery(window).on("load", function() {
                $('#box').load('handle.php');
            });
        }


    })
    </script>

</body>

</html>

Here is the handle.php file content

<?php

 echo '<h3>Data from handle.php</h3>';

CodePudding user response:

Solution

$(document).ready(function(){

    jQuery(window).on("load", function(){

     $('#box').load('handle.php');
 
     refreshInterval = setInterval(function(){
        $('#box').load('handle.php');
     },40000);
 
    });
})
  • Related