patternjavascriptMinor
Calling PHP file using Ajax
Viewed 0 times
fileajaxphpusingcalling
Problem
I like to know if there will be any performance issue if an
or
In the first example, the
In the second example, the object is created once.
xmlhttp object is created every time. For example, in the below code:function currentTime(){
var obj = null;
if(window.XMLHttpRequest){
obj = new XMLHttpRequest();
} else {
obj = new ActiveXObject("Microsoft.XMLHTTP");
}
obj.open("GET", "current_time.php", true);
obj.send(null);
obj.onreadystatechange = function(){
if(obj.readyState == 4 && obj.status == 200){
var element = document.getElementById("current_time");
element.innerHTML = obj.responseText;
}
};
}
setInterval("currentTime();", 1000);or
var obj = null;
function createObj(){
if(window.XMLHttpRequest){
obj = new XMLHttpRequest();
} else {
obj = new ActiveXObject("Microsoft.XMLHTTP");
}
}
function currentTime(){
obj.open("GET", "current_time.php", true);
obj.send(null);
obj.onreadystatechange = function(){
if(obj.readyState == 4 && obj.status == 200){
var element = document.getElementById("current_time");
element.innerHTML = obj.responseText;
}
};
}
createObj();
setInterval("currentTime();", 1000);In the first example, the
XMLHttpRequest object is created every time (1 sec).In the second example, the object is created once.
Solution
But the second version has problems also because the
You could also pass the location of the PHP file to this
obj variable is in the global namespace. It might be a good idea to abstract this into a function which contains a reference to the XMLHttpRequest object, and avoids polluting globals as in the first example.var CurrentTime = function() {
this.requestObj = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
this.element = document.getElementById("current_time");
};
CurrentTime.prototype.get = function() {
var self = this;
this.requestObj.open("GET", "current_time.php", true);
this.requestObj.send(null);
this.requestObj.onreadystatechange = function() {
if (self.requestObj.readyState == 4 && self.requestObj.status == 200) {
self.element.innerHTML = self.requestObj.responseText;
}
};
};
var currentTime = new CurrentTime();
setInterval(currentTime.get.apply(currentTime), 1000);You could also pass the location of the PHP file to this
CurrentTime function for further abstraction, however I think this is good as is. The apply method is used to set the context of the method to the currentTime variable and not the window, so that you don't get TypeErrors. :)Code Snippets
var CurrentTime = function() {
this.requestObj = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
this.element = document.getElementById("current_time");
};
CurrentTime.prototype.get = function() {
var self = this;
this.requestObj.open("GET", "current_time.php", true);
this.requestObj.send(null);
this.requestObj.onreadystatechange = function() {
if (self.requestObj.readyState == 4 && self.requestObj.status == 200) {
self.element.innerHTML = self.requestObj.responseText;
}
};
};
var currentTime = new CurrentTime();
setInterval(currentTime.get.apply(currentTime), 1000);Context
StackExchange Code Review Q#60233, answer score: 3
Revisions (0)
No revisions yet.