user5756014
user5756014

Reputation: 301

How to Run Ajax request automatically after a specific time?

I have an Ajax function (ajaxRequest) that I want to run automatically after some specific time (say 5 sec) without any user-prompt or warning . How can i do this? (I looked in the following links but found no solution )

jQuery: How to make an AJAX request and continue without waiting for request to complete?

How do I run ajax request in background continuously?

How to run ajax call in background after some particulate time?

How to run execute page in background using ajax?

//My Code
//ajaxRequest Script..

<script type="text/javascript">
function ajaxRequest(map){

var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", 'json_data.php', true);

xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    var myArr = JSON.parse(xmlhttp.responseText);
    setMarkers(map,myArr);
}
};
xmlhttp.send(null);
}
</script>

<script>
function myMap() {
var mapCanvas = document.getElementById("map");
var myCenter = new google.maps.LatLng(51.508742,-0.120850);
var lat=51.508742;
var lng=-0.140850;
var mapOptions = {center: myCenter, zoom: 5};
var map = new google.maps.Map(mapCanvas,mapOptions);

ajaxRequest(map);                                   //Called ajaxRequest

}
</script>

Upvotes: 2

Views: 4809

Answers (6)

prasanth
prasanth

Reputation: 22490

after run mymap() function the ajax request trigger in 5sec.

if You need ajax run at the time page load apply like this:

<script>
setTimeout(function() {ajaxRequest(map);},5000);
</script>

directly place with in a script

<script type="text/javascript">
function ajaxRequest(map){

var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", 'json_data.php', true);

xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    var myArr = JSON.parse(xmlhttp.responseText);
    setMarkers(map,myArr);
}
};
xmlhttp.send(null);
}
</script>

<script>
function myMap() {
var mapCanvas = document.getElementById("map");
var myCenter = new google.maps.LatLng(51.508742,-0.120850);
var lat=51.508742;
var lng=-0.140850;
var mapOptions = {center: myCenter, zoom: 5};
var map = new google.maps.Map(mapCanvas,mapOptions);

setTimeout(function() {ajaxRequest(map);},5000);    //Called ajaxRequest

}
</script>

Upvotes: 2

Nikhil Vaghela
Nikhil Vaghela

Reputation: 2096

Try this...

setTimeout(function() {
//post your ajax code here
}, 1000);

For more check here http://www.w3schools.com/jsref/met_win_setinterval.asp

Upvotes: 1

Jaydip Bhingradiya
Jaydip Bhingradiya

Reputation: 338

Hello this is solution for the automatically send the ajax request

Just put your function like this.

function myMap() {
var mapCanvas = document.getElementById("map");
var myCenter = new google.maps.LatLng(51.508742,-0.120850);
var lat=51.508742;
var lng=-0.140850;
var mapOptions = {center: myCenter, zoom: 5};
var map = new google.maps.Map(mapCanvas,mapOptions);                     
setInterval(function()
    {
      ajaxRequest(map);
    }, 5000);
}

Upvotes: 2

Anthony
Anthony

Reputation: 2122

If you want execute your code once you can use setTimeout(), if you want execute your code every x seconds you can use setInterval().

setTimeout(function() {
    //The code you want to execute after 5 seconds
}, 5000);
//OR
setInterval(function() {
    //The code you want to execute every 5 seconds  
}, 5000);

Upvotes: 3

atul
atul

Reputation: 134

If you want to execute the ajax call after every 5 seconds, try following:

setInterval (function() { ajaxRequest(map); }, 5000);

Upvotes: 2

Pranesh Ravi
Pranesh Ravi

Reputation: 19113

Use setTimeout(fn, time) to call the function once after a specified time(ms) or use setIntreval(fn, time) to call the function repeatedly for a specified time

Upvotes: 1

Related Questions