Skip to content
Advertisement

About auto refresh a part of in the php file

I want to test auto refresh in a php file. The following I’d the code. However, I cannot do it. I don’t know what is the problem?

<html>
<head>
<title>TEST</title>
</head>
<body>
<script type="text/javascript">
    window.onload = setupRefresh;
    function setupRefresh()
    {
        setInterval("refreshBlock();",1000);
    }

    function refreshBlock()
    {
       $('#test').load("test.php");
    }
  </script>

<div id="test">
    <?php echo rand(); ?>
</div>

</body>
</html>

Advertisement

Answer

Do like that.

<html>
<head>
<title>TEST</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
    window.onload = setupRefresh;
    function setupRefresh()
    {
        setInterval(refreshBlock,1000); //Call function like this
    }

    function refreshBlock()
    {
       $('#test').load("test.php");
    }
  </script>

<div id="test">
    <?php echo rand(); ?>
</div>

</body>
</html>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement