Reputation: 303
Suppose I have the following JavaScript code running in a Firefox plugin, which should perform an XmlHttpRequest
on a page load:
function my_fun(){
var xmlHttpConnection = new XMLHttpRequest();
xmlHttpConnection.open('GET', globalUrl+'/call_this.php', true);
xmlHttpConnection.onreadystatechange=function(){
if (xmlHttpConnection.readyState == 4 && xmlHttpConnection.status == 200) {
var serverResponse = xmlHttpConnection.responseText;
do_stuff();
clearTimeout(xmlHttpTimeout);
xmlHttpConnection.abort();
}
};
xmlHttpConnection.send(null);
xmlHttpTimeout = setTimeout(function (){
do_other_stuff();
xmlHttpConnection.abort();
clearTimeout(xmlHttpTimeout);
},5000);
}
container.addEventListener("load", my_fun, true);
When my_fun()
is called, the access logs for my Apache server look something like this:
<client-ip> - - [06/Nov/2014:10:40:04 -0500] "GET /call_this.php HTTP/1.1" 200 1 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0"
Further suppose I have 4 client connections on my server. Within the span 1 second, this line can show up in the access log dozens (sometimes over 50) of times simultaneously, and the pings sometimes come from just 1 client. Most times, the number of pings per second is lower than 5, but there are occasional spikes caused by call_this.php
.
Why would this happen? I suspect something is wrong with container.addEventListener("load", my_fun, true);
. Does this over count the number of loads, e.g., if a page contains multiple hidden URL's that are loaded? If so, how can I fix this? (Edit: Would an observer service be better?)
Upvotes: 0
Views: 237
Reputation: 303
Found the answer. I do believe there is a problem with the event listener. But since the above code is in a Firefox plugin, a good approach is to use Mozilla observers. The following observer significantly reduced the number of pings to the server (empirically tested).
Special thanks goes to Noitidart, who supplied a similar solution to a different problem.
var myObserver = {
register: function() {
var observerService = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
observerService.addObserver(this, TOPIC_MODIFY_REQUEST, false);
},
//observe function to capture the changed event
observe : function(aSubject, aTopic, aData) {
if (TOPIC_MODIFY_REQUEST == aTopic ) {
var url;
aSubject.QueryInterface(Components.interfaces.nsIHttpChannel);
url = aSubject.URI.spec;
url = encodeURIComponent(url);
var oHttp = aSubject.QueryInterface(Components.interfaces.nsIHttpChannel);
if (oHttp.loadFlags & Components.interfaces.nsIHttpChannel.LOAD_INITIAL_DOCUMENT_URI) {
//is top level load
my_fun(); //Same function as in original question
}
}
}
}
myObserver.register();
Upvotes: 1
Reputation: 1278
Yes. something looks wrong to me:
Check this line:
container.addEventListener("load", function(){my_fun();}, true);
or change function declaration:
function my_fun(){
...
for
my_fun = function(){
...
Upvotes: 2