counter.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Protect window.console method calls, e.g. console is not defined on IE
  3. * unless dev tools are open, and IE doesn't define console.debug
  4. *
  5. * http://stackoverflow.com/questions/3326650/console-is-undefined-error-for-internet-explorer
  6. */
  7. (function () {
  8. if (!window.console) {
  9. window.console = {};
  10. }
  11. // union of Chrome, FF, IE, and Safari console methods
  12. var m = [
  13. "log", "info", "warn", "error", "debug", "trace", "dir", "group",
  14. "groupCollapsed", "groupEnd", "time", "timeEnd", "profile", "profileEnd",
  15. "dirxml", "assert", "count", "markTimeline", "timeStamp", "clear"
  16. ];
  17. // define undefined methods as noops to prevent errors
  18. for (var i = 0; i < m.length; i++) {
  19. if (!window.console[m[i]]) {
  20. window.console[m[i]] = function () {
  21. };
  22. }
  23. }
  24. })();
  25. (function () {
  26. var Counter = function (server) {
  27. if (!server) {
  28. server = 'http://counter.duowan.com';
  29. }
  30. this._server = server;
  31. };
  32. Counter.prototype.incr = function (key, step) {
  33. if (!key) {
  34. key = 'clickOnPage';
  35. }
  36. if (!step) {
  37. step = 1;
  38. }
  39. var counter = this;
  40. var data = {
  41. domain: 'lolbox',
  42. subkey: key,
  43. count: step
  44. };
  45. var options = {
  46. type:'POST',
  47. url:counter._server + '/open/daily/collect',
  48. crossDomain: true,
  49. data: data,
  50. timeout:3 * 1000,
  51. cache: false
  52. };
  53. $.ajax(options)
  54. .done(
  55. function (data, textStatus, jqXHR) {
  56. if (jqXHR.status !== 200) {
  57. console.log('counter incr failed');
  58. }
  59. })
  60. .fail(
  61. function (jqXHR, textStatus, errorThrown) {
  62. console.error(jqXHR);
  63. console.error(textStatus);
  64. console.error(errorThrown);
  65. });
  66. };
  67. window.Counter = window.Counter ? window.Counter : Counter;
  68. })();