Skip to content

Instantly share code, notes, and snippets.

@aNd1coder
Created March 19, 2016 05:44
Show Gist options
  • Save aNd1coder/29c76388158517b29cbb to your computer and use it in GitHub Desktop.
Save aNd1coder/29c76388158517b29cbb to your computer and use it in GitHub Desktop.
用全局变量接收创建的Image对象,防止临时变量被回收而导致统计数据丢失
// http://oldj.net/article/one-thing-to-notice-about-new-image/
var img = new Image();
var rnd_id = "_img_" + Math.random();
window[rnd_id] = img; // 全局变量引用
img.onload = img.onerror = function () {
window[rnd_id] = null; // 删除全局变量引用
}
img.src = "http://log.mysite.com/1.gif?a=1&b=2&c=xxx";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment