Javascript Таймер с вводом - PullRequest
0 голосов
/ 30 мая 2020

Я пытаюсь создать интерактивный таймер, который может принимать ввод. Как мне добавить ввод в этот код ниже, чтобы я мог получить время (например, в минутах) от пользователя, чтобы я мог рассчитать таймер?

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet">
    <link href="styles.css" rel="stylesheet">
    <title>Timer</title>
</head>
<script src='homepage.js'></script>
<body>
    <nav aria-label="breadcrumb">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="index.html">Home</a></li>
            <li class="breadcrumb-item active" aria-current="page">Timer</li>
          </ol>
    </nav>
    <center>
        <div>
            <form onsubmit="timer(); return false;">
                <input type="submit" value='Start' class='button button1'>
            </form>
            <h1 id='demo'></h1>
            <form onsubmit="stopper();">
                <input type="submit" value='Stop' class='button button2'>
            </form>
        </div>
    </center>
</body>

Javascript

function stopper() {
    let stop = true;
}

let stop = false;
function timer() {

    // Set the date we're counting down to
    var countDownDate = new Date("Jan 5, 2021 15:37:25").getTime();

    // Update the count down every 1 second
    var x = setInterval(function() {

        // Get today's date and time
        var now = new Date().getTime();

        // Find the distance between now and the count down date
        var distance = countDownDate - now;

        // Time calculations for days, hours, minutes and seconds
        var days = Math.floor(distance / (1000 * 60 * 60 * 24));
        var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
        var seconds = Math.floor((distance % (1000 * 60)) / 1000);

        // Display the result in the element with id="demo"
        document.getElementById("demo").innerHTML = days + " : " + hours + " : " + minutes + " : " + seconds;

        // If the count down is finished, write some text
        if (distance < 0) {
            document.getElementById("demo").innerHTML = 'Times up!';
            alert('Times up!');
        }
    }, 1000);
    if (stop === true) {
        return;
    }
}

------------------ -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ----------------

1 Ответ

1 голос
/ 30 мая 2020

Попробуйте следующий фрагмент, заменив console.log фактическим logi c.

setTimeout(() => {
    console.log('logic timedout')
}, parseInt(prompt()) * 1000)
...