Home > Web Front-end > JS Tutorial > Using SetTimeout to Make a jQuery Timer

Using SetTimeout to Make a jQuery Timer

Joseph Gordon-Levitt
Release: 2025-03-06 01:11:10
Original
301 people have browsed it

Using SetTimeout to Make a jQuery Timer

This tutorial demonstrates a jQuery countdown timer that decrements from 10 every second. This is achieved by recursively calling setTimeout within the timer function. Below is the code.

jQuery Code

jQuery(document).ready(function() {

    let countdown;
    let countdownNumber;

    function countdownInit() {
        countdownNumber = 11;
        countdownTrigger();
    }

    function countdownTrigger() {
        if (countdownNumber > 0) {
            countdownNumber--;
            $('#countdown_text').html(countdownNumber); //Corrected selector and method
            if (countdownNumber > 0) {
                countdown = setTimeout(countdownTrigger, 1000); //Simplified setTimeout call
            }
        }
    }

    function countdownClear() {
        clearTimeout(countdown);
    }

    countdownInit(); //Start the timer on page load

});
Copy after login

HTML Code

<div id="countdown_text">Placeholding text</div>
Copy after login

Frequently Asked Questions about jQuery Timers

This section addresses common questions regarding jQuery timers.

Q: How do I build a countdown timer with jQuery?

A: Use setInterval for repeated execution at a set interval. Here's a concise example:

let count = 10;
let counter = setInterval(function() {
    count--;
    if (count < 0) {
        clearInterval(counter);
        return;
    }
    $("#timer").text(count + " secs");
}, 1000);
Copy after login

Q: How can I pause a jQuery timer?

A: Use clearInterval to stop a timer started with setInterval.

function pauseTimer() {
    clearInterval(counter);
}
Copy after login

Q: How to resume a paused jQuery timer?

A: JavaScript doesn't directly resume timers. Restart it with setInterval using the current count value.

function resumeTimer() {
    counter = setInterval(function() {
        count--;
        if (count < 0) {
            clearInterval(counter);
            return;
        }
        $("#timer").text(count + " secs");
    }, 1000);
}
Copy after login

Q: How to create a delayed function execution in jQuery?

A: jQuery's .delay() method adds a delay before executing a function within an animation chain.

$("#myDiv").delay(2000).fadeIn(400);
Copy after login

Q: How to create a repeating timer in jQuery?

A: setInterval is the key for repeatedly executing a function.

setInterval(function() {
    alert("Hello");
}, 3000);
Copy after login

The above is the detailed content of Using SetTimeout to Make a jQuery Timer. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template