load_timer.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. App.loadTimer = Em.Object.create({
  19. timeStampCache: {},
  20. /**
  21. * save start timestamp
  22. * @param {string} label
  23. */
  24. start: function(label) {
  25. $('.alert.attach-to-bottom-right').remove();
  26. if (App.get('supports.showPageLoadTime')) {
  27. this.get('timeStampCache')[label] = window.performance.now();
  28. }
  29. },
  30. /**
  31. * calculate time difference
  32. * @param {string} label
  33. * @returns {undefined|string}
  34. */
  35. finish: function(label) {
  36. var result;
  37. if (typeof(this.get('timeStampCache')[label]) === "number") {
  38. result = Number(window.performance.now() - this.get('timeStampCache')[label]).toFixed(2);
  39. console.debug(label + " loaded in: " + result + "ms");
  40. this.display(label + " loaded in: " + result + "ms");
  41. delete this.get('timeStampCache')[label];
  42. }
  43. return result;
  44. },
  45. /**
  46. * display time results on the screen
  47. * @param {string} result
  48. */
  49. display: function(result) {
  50. var alert = $("<div class='alert attach-to-bottom-right'>" + result + "</div>");
  51. var closeButton = $("<i class='icon-remove-circle'></i>").click(function () {
  52. $(this).remove();
  53. $(alert).remove();
  54. });
  55. alert.append("&nbsp;", closeButton);
  56. $('body').append(alert);
  57. }
  58. });