倒计时后如何提交表格

时间:2016-05-06 20:31:12

标签: javascript html

我有点陷入我的项目我已经能够使用JavaScript获得倒计时功能,我现在面临的问题是我不知道在哪里设置一个函数,一旦它为0: 00提交表格

这是html:

      <form name = 'countdown' >
      </form>

这是Javascript

    (function($) {
$.tinyTimer = function(options) {
    var tick, tt = this, elem = (tt.options = options).element, ref = new Date(options.from || options.to).getTime(), dir = !!options.from || -1, M = Math, doNothing = function() {};
    tt.interval = setInterval(tick = function() {
        if (!tt.paused) {
            var sec = M.max(M.round((Date.now() - ref) * dir / 1e3), 0), val = {
                S: sec,
                s: sec % 60,
                M: M.floor(sec /= 60),
                H: M.floor(sec /= 60),
                D: M.floor(sec /= 24)
            };
            val.m = val.M % 60, val.h = val.H % 24, val.d = val.D, val.text = (options.format || "%-H{:}%0m:%0s").replace(/%(-?)(0?)([dhms])(\s*)(?:\{(.+?)\})?/gi, options.replacer || function(match, omit, zero, part, space, forms) {
                var v = val[part];
                return (forms = (forms || "").split("|"))[2] = forms[2] || (forms[1] = forms[1] || forms[0]), 
                !v && omit ? "" : (v > 9 ? "" : zero) + v + space + forms[+(1 != v) + (1 != v && (2 > v % 10 || v % 10 > 4) || v > 10 && 20 > v)];
            }), elem ? $(elem).html(val.text) : elem = tt, (options.onTick || doNothing).call(elem, tt.val = val), 
            0 > dir && !sec && (clearInterval(tt.interval), (options.onEnd || doNothing).call(elem, val));
        }
    }, 1e3), tick(), tt.pause = tt.stop = function() {
        tt.paused = Date.now();
    }, tt.resume = function() {
        ref -= (tt.paused - Date.now()) * dir, tt.paused = 0;
    }, tt.start = function() {
        tt.paused = 0;
    };
}, $.fn.tinyTimer = function(options) {
    return this.each(function() {
        $(this).data("tinyTimer", new $.tinyTimer($.extend(options, {
            element: $(this)
        })));
    });
};
})(jQuery);

var d = new Date();
d.setMinutes(d.getMinutes() + 5);
$('#timer').tinyTimer({ to: d });

请提供任何帮助。谢谢

2 个答案:

答案 0 :(得分:2)

使用超时:

var varTimerInMiliseconds = 5000;
setTimeout(function(){ 
    document.getElementById("myForm").submit();
}, varTimerInMiliseconds);

答案 1 :(得分:0)

尝试以下代码:

<html>
<head>
<script type="text/javascript">
window.onload=counter;
function counter()
{
minutes=02
seconds =60;
countDown();
}
</script>
<script type="text/javascript">
function countDown(){
document.getElementById("min").innerHTML= minutes;
document.getElementById("remain").innerHTML= seconds;
setTimeout("countDown()",1000);
	if(minutes == 0 && seconds == 0)
		{ 
		   
			//document.form.submit();
      document.write("form submitted"); 
		}
	else	
		{
		seconds--;	
		if(seconds ==0 && minutes > 0)
		{
			minutes--;
			seconds=60;
		}
		}
}
</script>
</head>
<body>
    <b>You have:</b>
		<span id="min" ></span> <b>Minutes</b>
		<span id="remain"></span> <b>Seconds</b>
</body>
</html>