Im creating a timer to display when someone record audio using webrtc.. I have this function working already.What i want to add is stop functionality so whenever the user click stop then the timer also set on timeout. Below is the example code..
// record start time
var startTime;
function display() {
// later record end time
var endTime = new Date();
// time difference in ms
var timeDiff = endTime - startTime;
// strip the miliseconds
timeDiff /= 1000;
// get seconds
var seconds = Math.round(timeDiff % 60);
// remove seconds from the date
timeDiff = Math.floor(timeDiff / 60);
// get minutes
var minutes = Math.round(timeDiff % 60);
// remove minutes from the date
timeDiff = Math.floor(timeDiff / 60);
// get hours
var hours = Math.round(timeDiff % 24);
// remove hours from the date
timeDiff = Math.floor(timeDiff / 24);
// the rest of timeDiff is number of days
var days = timeDiff;
$(".time").text(days + " days, " + hours + ":" + minutes + ":" + seconds);
setTimeout(display, 1000);
}
$("input#button").click(function () {
startTime = new Date();
setTimeout(display, 1000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<input type="button" id="button" value="Start" />
</div>
<div class="time"></div>
Any help would be appreciated. Thanks..