// Stopwatch functions
let startTime;
// Function to start the stopwatch
function startStopwatch() {
startTime = new Date();
}
// Function to stop the stopwatch and calculate elapsed time
function stopStopwatch() {
const endTime = new Date();
const elapsedTime = endTime - startTime;
console.log('Elapsed Time (ms):', elapsedTime);
}
// Starting the stopwatch
startStopwatch();
// Performing some tasks...
// Stopping the stopwatch after performing tasks
stopStopwatch();
code
Stopwatch in JavaScript
This JavaScript code demonstrates a simple stopwatch functionality. The startStopwatch function is used to record the start time, and the stopStopwatch function calculates the elapsed time by subtracting the start time from the current time. The elapsed time is then logged to the console in milliseconds.
Stopwatch
Timer