Eddie Lawrence
Eddie Lawrence

Reputation: 53

Getting an AJAX call to reload a div every x minutes

I'm new to Javascript and AJAX so please excuse my newb question. I have an AJAX call (that is working) and I'd like to have it refresh the contents of a div every 5 minutes (I'm not using jquery). This is how I'm calling the AJAX function in the <head></head> of my html page:

    <script type=text/javascript>
        setInterval(ajaxCall(), 300000);
    </script>

The initial page load populates the div, but the content of the div doesn't refresh after 5 minutes. What am I doing wrong?

Upvotes: 1

Views: 2294

Answers (3)

Andy Gaskell
Andy Gaskell

Reputation: 31761

Change your code to this:

setInterval(ajaxCall, 300000);

To pass an argument:

setInterval(function(){ ajaxCall(someCoolValue); }, 300000);

Notice the lack of parentheses in ajaxCall. You want to pass the function itself, not call the function. More examples on MDN.

Upvotes: 5

Adarsh Vardhan
Adarsh Vardhan

Reputation: 257

Here

<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
</head>

<div id="links">

</div>

<script language="javascript" type="text/javascript">
var timeout = setTimeout(reloadChat, 300000);

function reloadChat () {
$('#links').load('test.php #links',function () {
        $(this).unwrap();
        timeout = setTimeout(reloadChat, 300000);
});
}
</script>

Also you are calling it wrong correct syntax would be ,

setInterval(ajaxCall, 300000);

Hope it helps :D

Upvotes: 0

Zee
Zee

Reputation: 8488

You need to wrap your ajaxCall.

<script type=text/javascript>
    setInterval(function(){ ajaxCall(); }, 300000);
</script>

Upvotes: 1

Related Questions