DeveloperBreeze

Timer Development Tutorials, Guides & Insights

Unlock 1+ expert-curated timer tutorials, real-world code snippets, and modern dev strategies. From fundamentals to advanced topics, boost your timer skills on DeveloperBreeze.

Stopwatch in JavaScript

Code January 26, 2024
javascript

// 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();