A few days ago, I wanted to add a draggable electronic clock to the upper right corner of my personal homepage. Then, while I had some free time these days, I briefly reviewed object-oriented programming, so I spent some time on it. It came out, and it looks like this:
In fact, this case is very simple, it is just a simple use of Date class and timer. Of course, this example is written just to review the front-end knowledge. It is encapsulated into a component
The code is as follows:
(function (window, undefined) { function Time() { this.clock = null; this.date = new Date(); this.month = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; this.day = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Friday', 'Saturday']; this.str = '11:34 Tuesday30August '; } Time.prototype = { constructor: 'Time', init: function (selector) { this.createHTML(selector); this.getTime(); this.getDay(); }, createHTML: function (selector) { var elem = document.querySelector(selector); this.clock = elem; elem.innerHTML = this.str; }, getTime: function () { var clock = this.clock; var hour = clock.getElementsByClassName('cth_hour')[0]; var minute = clock.getElementsByClassName('cth_min')[0]; var point = clock.getElementsByClassName('cth_point')[0]; var date = this.date; var date_hour = this.addZoom(date.getHours()); //时 var date_minutes = this.addZoom(date.getMinutes()); //分 hour.innerHTML = date_hour; minute.innerHTML = date_minutes; var num = 0; var timer = setInterval(function () { if (num % 2 == 1) { point.style.visibility = 'visible'; } else { point.style.visibility = 'hidden'; } num++; }, 500); var This = this; var timer2 = setInterval(function () { var date = new Date(); hour.innerHTML = This.addZoom(date.getHours()); minute.innerHTML = This.addZoom(date.getMinutes()); }, 30000); }, getDay: function () { var clock = this.clock; var dates = clock.getElementsByClassName('cdm_date')[0]; var day = clock.getElementsByClassName('cdm_day')[0]; var month = clock.getElementsByClassName('cdm_month')[0]; var date = this.date; var date_day = date.getDay(); //星期 var date_date = date.getDate(); //日 var date_month = date.getMonth(); //月 dates.innerHTML = this.day[date_day]; day.innerHTML = this.addZoom(date_date); month.innerHTML = this.month[date_month * 1]; }, addZoom: function (para) { var str = ''; if (para < 10) { str = '0' + para; } else { str = para; } return str; } } DesktopTime = Time; })(window);
Of course, I wrote the function in a self-executing way. The advantage of this is that it avoids duplication of variables. Of course, the disadvantage is that it is difficult to get the value in the function, so through Assign the Time class to the global variable DesktopTime, and change the ## of Time #prototypeIndirectly exposed, and then called to initialize.
Related recommendations:
Use html5 to implement a simple clock appearance
JavaScript to implement a clock
The above is the detailed content of JavaScript implements encapsulation of simple electronic clock component code. For more information, please follow other related articles on the PHP Chinese website!