반응형
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | /* main.js */ var _TimerModule = require('./timer'); var _TextModule = require('./text'); var TimerModule = new _TimerModule(); /* Need Allocate Object */ var TextModule = new _TextModule(); /* Test Code */ var _testModule1 = function () { return new Promise(function (resolve, reject) { TimerModule.setTimer(0, 1000); resolve(); }); }; var _testModule2 = function () { return new Promise(function (resolve, reject) { TextModule.setText("사랑해요... 지성군!", TimerModule); resolve(); }); }; var _testModule3 = function () { var lastTick; var Update = function () { var currentTick = new Date().getTime(); var elapseTick = currentTick - (lastTick || currentTick); console.log(TextModule.readText(elapseTick)); lastTick = currentTick; } return new Promise(function (resolve, reject) { setInterval(Update, 100); resolve(); }); }; Promise.all([_testModule1(), _testModule2(), _testModule3()]).then(function () { console.log("All Completed"); }); | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | /* timer.js */ (function () { "use strict" function TimerModule() { this.timer = { time: 0, get: function () { return this.time; }, set: function (_time) { this.time = _time; }, tick: function (_tick) { this.time += _tick; } }; this.startTimer = 0; this.endTimer = 1000; } TimerModule.prototype.setTimer = function (_startTime, _endTime) { this.timer.set(_startTime); this.startTimer = _startTime; this.endTimer = _endTime; }; TimerModule.prototype.isTick = function () { return (this.timer.get() > this.endTimer); }; TimerModule.prototype.addTick = function (_tick) { this.timer.tick(_tick); }; TimerModule.prototype.resetTick = function (_endTime) { this.timer.set(this.startTimer); this.endTimer = _endTime; }; module.exports = TimerModule; })() | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | /* text.js */ (function () { "use strict" function TextModule() { this.text = ""; this.currentIndex = 1; // Notifycation <text.js:24> this.timerModule; this.TypingText = function () { if (this.timerModule.isTick()) { switch (this.text[this.currentIndex++]) { case '.': this.timerModule.resetTick(500); // millisecond break; case ' ': case '?': case '!': this.timerModule.resetTick(800); break; default: this.timerModule.resetTick(1000); break; } } /* Warning Grammer < && ( interger(0) is false ) || > */ return this.text.substring( 0, (this.currentIndex <= this.text.length && this.currentIndex || this.text.length) - 1); } } TextModule.prototype.setText = function (_text, _timerModule) { /* Warning */ if (typeof (_text) != "string" || typeof (_timerModule) != "object") { console.log("TypeError: string & TimerModule <text.js:31>"); return; } this.text = _text; this.timerModule = _timerModule; }; TextModule.prototype.readText = function (_elapseTick) { this.timerModule.addTick(_elapseTick); return this.TypingText(); }; module.exports = TextModule; })() | cs |
반응형
'언어 > js' 카테고리의 다른 글
VS 2022 에서 js 개발 - (1) (0) | 2023.04.23 |
---|---|
ECMAScript 6 Tutorial (0) | 2016.02.26 |
closure & promise (0) | 2016.02.23 |
closure (0) | 2016.02.23 |
Module Pattern (0) | 2016.02.23 |