Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function StopWatch() {
- let duration = 0;
- Object.defineProperty(this, 'duration', {
- get: function () {
- return duration;
- }
- });
- let status;
- let interval;
- this.start = function () {
- if (status == 'started')
- throw Error('Already started!');
- interval = setInterval(timer, 1000);
- status = 'started';
- }
- const timer = function () {
- duration++;
- }
- this.stop = function () {
- if (status == 'stopped' || (typeof status) == 'undefined')
- throw Error('Already stopped!')
- clearInterval(interval);
- status = 'stopped';
- }
- }
- const sw = new StopWatch();
Add Comment
Please, Sign In to add comment