0

I have a problem that when I was using image.src to make a request on a 1x1 gif. But when I use chrome and safari, the request cannot be logged in my web server. But when I use a javascript to delay for some mini-second(let's say 300ms), it works then. Do any one know a better solution instead of using 300ms delay(as it makes my click become slower)?

My javascript looks like that

/* Event Capture */
function eventCapture(et,ep,eid,eurl) {     
  var ec=new Image();
  ec.src=cp+cd+cu
  +"&et="+escape(et)
  +"&ep="+escape(ep)
  +"&ei="+escape(eid)
  +"&eu="+escape(eurl)+_do+_vo
  +"&cb="+new Date().getTime();   
}

Does anyone know the reqson?

2 Answers2

0

How are you setting the delay? If you just call

setTimeout("javascript function",milliseconds);

on the event capture and put the delay in there, it shouldn't block the rest of your page and will be able to handle multiple clicks at once since each call would call a new setTimeout that is independent from the rest.

As far as your code is concerned, I'm assuming the variables cp, cd, and cu are defined globally elsewhere? If something isn't defined when the script is first called, Chrome will throw a ReferenceError while you're creating the src string of the image, which could be what is throwing everything off.

TMan
  • 1,775
  • 1
  • 14
  • 26
  • Thank you very much. The delay approach now using by vendor is by looping,something like this: void pausecomp(pausetime){ var wait_time = new Date() + pausetime; while (new Date() < wait_time) {}; } The variables are all global. – user1640775 Sep 02 '12 at 03:18
  • Sorry I know it has been a few days but was there a reason you couldn't call setTimeout()? – TMan Sep 05 '12 at 23:24
0

One possibility is that the image is garbage collected immediately because there's no lasting reference to the image variable ec. But, when you do a setTimeout(), it creates a closure that make the variable last long enough.

You could test this solution that creates a temporary closure only until the image actually loads to see if it solves the problem:

/* Event Capture */
function eventCapture(et,ep,eid,eurl) {     
  var ec=new Image();
  ec.onload = function() {
      ec = null;
  }

  ec.src=cp+cd+cu
  +"&et="+escape(et)
  +"&ep="+escape(ep)
  +"&ei="+escape(eid)
  +"&eu="+escape(eurl)+_do+_vo
  +"&cb="+new Date().getTime();   
}
jfriend00
  • 683,504
  • 96
  • 985
  • 979