Newboy11
Newboy11

Reputation: 3136

Trying to use setInterval() but error occurs

I'm trying to load testdata.php and automatically refresh every 10 seconds. But it doesn't work. What's wrong with my code? I'm still new at web programming so please bear with me

<html>
<head>
<script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0   /jquery.min.js">
$( "#data" ).load( "http://localhost/testdata.php" );
</script>    
</head>

<body>

<div id="data"></div>    

</body>
window.setInterval(function(){
/// call your function here
}, 10000);      
</html>

Upvotes: 1

Views: 51

Answers (1)

Nergal
Nergal

Reputation: 1015

Okay, so there was a few space in the URL of jquery which means the browser couldn't load it. The load wasn't inside the interval and your javascript code should be inside a script tag. And be sure you call jQuery when the document is ready.

<html>
<head>
<script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script type="text/javascript">
    $( document ).ready(function(){
        $( "#data" ).load( "http://localhost/testdata.php" );
        setInterval(function(){
            $( "#data" ).load( "http://localhost/testdata.php" );
        }, 10000);  
    });   
</script>
</head>

<body>

<div id="data"></div>    

</body> 
</html>

Edit: Added a line to load the data when the dom is ready and then set the interval.

Upvotes: 2

Related Questions