Make stopwatch alive between request

Viewed 110

I am new to Ractive.js. I need to implement a stop watch which will be running even if I click for another page. My stopwatch is running but when I click to another page and then come back to my current page, the stopwatch is gone. Can anyone please help me on this please ? Here are my attempts below :

in my view >>>

<div class="modal-footer">
        <div class="form-group">
            <div class="pull-left">
                Stopwatch: {{#stopWatch}} {{hours}} h: {{minutes}} m: {{seconds}} s{{/stopWatch}}
            </div>
            <button id="btnStart" class="btn btn-primary" type="button" on-click="startStopWatch">Start</button>
            <button id="btnReset" class="btn btn-secondary" type="button" on-click="startStopWatch">Reset</button>
        </div>
        <label class="pull-left stopWatch">  Time Spent:</label>
    </div>

my startStopWatch function >>>

function startStopWatch(ev, cfg) {
    var rjs = this || cfg.rjs;
    var $btn = $(ev.node);
    var btnText = $btn.text();
    var sg;

    if (btnText === 'Start') {
        $btn.html('Stop');
        sg = new cmnDt.stopWatch();
        sg.start(rjs, sg);
    } else if (btnText === 'Stop'){
        $btn.html('Start');
        sg = ev.context.sw;
        sg.stop(rjs, sg);
    }

    if (btnText === 'Reset' && ev.context.sw) {
        sg = ev.context.sw;
        sg.reset(rjs, sg);
        $('#btnStart').html('Start');
    }
}

my stopWatch function >>>

function stopWatch() {

        var currentSeconds = 0;
        var currentMinutes = 0;
        var currentHours = 0;
        var timer;

        function start(rjs, sw) {
            printTime(rjs, sw);
            timer = setInterval(function () {
                getTime();
                printTime(rjs, sw);
            }, 1000);
        }

        function getTime(rjs) {
            currentSeconds = currentSeconds + 1;
            if (currentSeconds === 60) {
                currentMinutes += Math.round((currentSeconds) / 60);
                currentSeconds = 0;
            } else if (currentSeconds === 60 && currentMinutes === 60) {
                currentMinutes = 0;
                currentSeconds = 0;
                currentHours += 1;
            }
        }

        function printTime(rjs, sw) { 
            rjs.set({'sw': sw, 'stopWatch.seconds': currentSeconds, 
                    'stopWatch.minutes': currentMinutes, 'stopWatch.hours': currentHours});
        }

        function stop(rjs, sw) {
            clearInterval(timer);
            printTime(rjs, sw);
            $('.stopWatch').text(' Time Spent: ' + currentMinutes + ' m : ' + currentSeconds + ' s.');
        }

        function reset(rjs, sw) {
            clearInterval(timer);
            currentSeconds = 0;
            currentMinutes = 0;
            currentHours = 0;
            printTime(rjs, sw);
        }

        return {
            start: start,
            stop: stop,
            reset: reset
        };
    }
1 Answers
Related