showing results for - "auto form submition at countdown end javascript"
Flora
04 Jul 2017
1/*
2
3HTML needed in the body before Javascript
4<div id="demo"></div>
5<form method="post" id="username">
6<input type="text" name="usename" placeholder="Enter Username">
7</form>
8
9<?php
10
11if ($_SERVER['REQUEST_METHOD'] == 'POST) {
12echo $_POST['username'];
13}
14
15?>
16
17*/
18
19function timeRemaining (id, stoptime) {
20      // Set the date we're counting down to
21      var countDownDate = new Date(stoptime).getTime();
22
23      // Update the count down every 1 second
24      var x = setInterval(function() {
25
26        // Get today's date and time
27        var now = new Date().getTime();
28
29        // Find the distance between now and the count down date
30        var timediffer = countDownDate - now;
31
32        // Time calculations for days, hours, minutes and seconds
33        var days = Math.floor(distance / (1000 * 60 * 60 * 24));
34        var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
35        var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
36        var seconds = Math.floor((distance % (1000 * 60)) / 1000);
37
38        // Output the result in an element with id="demo"
39        document.getElementById(id).innerHTML = days + "d " + hours + "h "
40            + minutes + "m " + seconds + "s ";
41
42        // If the count down is over, write some text
43        if (timediffer < 0) {
44          clearInterval(x);
45          $('#username').submit();
46          document.getElementById(id).innerHTML = "Time-up Form is submiting";
47        }
48
49      }, 1000);
50    }
51    timeRemaining('demo', 'Dec 2, 2020 14:21:00');