Search code examples
jquerystopwatch

timer resume issue in jquery


I have one existing code of stopwatch. and it is working fine but it has functionality that when I click on pause button it changes start button value with Resume and make timer pause. and when I click the start button which has value Resume stopwatch resume and start timer where it pauses.

I have created my code in Jsfiddle here

and I want my start button value as it is and I have to make another resume button with which I have to make resume functionality.

I have made second resume functionality and don't know how can I do this.

actually, I want to start my stopwatch directly from a particular time.

that's why I want code through which I can start my timer from particular time.

can anybody help me in this.

<input type="button" value="Start" id="sw_start" />
<input type="button" value="Resume" id="resume" /> // this button made by me
<input type="button" value="Pause" id="sw_pause" />
<input type="button" value="Stop"  id="sw_stop" />
<input type="button" value="Reset" id="sw_reset" />

Solution

  • JSFiddle: https://jsfiddle.net/9xeqskro/

    I've edited the JSFiddle and added a simple function called startFromTime which takes a parameter of milliseconds and adjusts a couple of settings in the APP plugin to start from a specified time. How's that?

    The code I added is as follows:

    function startFromTime(ms) {
        // manually set time difference to desired input
        $.APP.td = parseInt(ms);
    
        // set state to 'pause' to mimick 'resume' behaviour
        $.APP.state = 'pause';
    
        // start the timer
        $.APP.startTimer('sw');
    }
    
    // call startFromTime() using manual number input
    $('#start_custom').on('click', function() {
        var ms = $('#start_time').val();
        startFromTime(ms);
    });