Delay a snippet of code
var ajaxRequestqqq; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequestqqq = new XMLHttpRequest();
} catch (e){
// Internet Expl开发者_运维知识库orer Browsers
try{
ajaxRequestqqq = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequestqqq = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequestqqq.onreadystatechange = function(){
if(ajaxRequestqqq.readyState == 4){
var ajaxDisplayqqq = document.getElementById('rajax');
ajaxDisplayqqq.innerHTML = ajaxRequestqqq.responseText;
}
}
var rurlvar = $("#rurl").val();
var rnameval = $("#rname").val();
var queryStringqqq = "url=" + rurlvar + "&name=" + rnameval;
var urltofileqqq = "subrssajax.php";
ajaxRequestqqq.open("POST", urltofileqqq, true);
ajaxRequestqqq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxRequestqqq.setRequestHeader("Content-length", queryStringqqq.length);
ajaxRequestqqq.setRequestHeader("Connection", "close");
ajaxRequestqqq.send(queryStringqqq);
***#DELAY#***
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('vrname');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
//var rurlvar = $("#rurl").val();
//var rnameval = $("#rname").val();
//var queryString = "url=" + rurlvar + "&name=" + rnameval;
var queryString = "url=sss";
var urltofile = "rname.php";
ajaxRequest.open("POST", urltofile, true);
ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxRequest.setRequestHeader("Content-length", queryString.length);
ajaxRequest.setRequestHeader("Connection", "close");
ajaxRequest.send(queryString);
How to set delay between the two requests?
You should use setTimeout
and refactor your code, something like this:
function createRequest() {
var ajaxRequest;
try {
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e) {
// Internet Explorer Browsers
try {
ajaxRequestqqq = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
ajaxRequestqqq = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
return ajaxRequest;
}
function setResponse(request, elementId) {
request.onreadystatechange = function() {
if (request.readyState == 4) {
var ajaxDisplay = document.getElementById(elementId);
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
}
function request1() {
var ajaxRequestqqq = createRequest(); // The variable that makes Ajax possible!
setResponse(ajaxRequestqqq, 'rajax');
var rurlvar = $("#rurl").val();
var rnameval = $("#rname").val();
var queryStringqqq = "url=" + rurlvar + "&name=" + rnameval;
var urltofileqqq = "subrssajax.php";
ajaxRequestqqq.open("POST", urltofileqqq, true);
ajaxRequestqqq.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
ajaxRequestqqq.setRequestHeader("Content-length", queryStringqqq.length);
ajaxRequestqqq.setRequestHeader("Connection", "close");
ajaxRequestqqq.send(queryStringqqq);
}
function request2() {
var ajaxRequest = createRequest(); // The variable that makes Ajax possible!
setResponse(ajaxRequest, 'vrname');
// var rurlvar = $("#rurl").val();
// var rnameval = $("#rname").val();
// var queryString = "url=" + rurlvar + "&name=" + rnameval;
var queryString = "url=sss";
var urltofile = "rname.php";
ajaxRequest.open("POST", urltofile, true);
ajaxRequest.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
ajaxRequest.setRequestHeader("Content-length", queryString.length);
ajaxRequest.setRequestHeader("Connection", "close");
ajaxRequest.send(queryString);
}
// Call first request
request1();
// Setup delay in milliseconds
var delay = 5000; // 5 seconds
setTimeout(request2(), delay);
精彩评论