在JS中设置一个计时器

时间:2014-05-05 12:49:58

标签: javascript jquery settimeout

我正在尝试在执行form.submit()之前设置超时但它不起作用。

$("#form").validate({
    submitHandler: function(form) {
        $("#form").hide();
        $('#form-inscription').append('<h2>Text</h2>');
        setTimout( function(){ form.submit() }, 10000 );
        //form.submit();
    }
});

2 个答案:

答案 0 :(得分:0)

var ticks = 0;
$("#form").validate({
    submitHandler: function(form) {
        $("#form").hide();
        $('#form-inscription').append('<h2>Text</h2>');
        ticks = 0;
        submitter(form); //this is your timer function 
    }
});

function submitter(form)
{
  if(ticks < 10)
  {
    ticks++;
    timer = setTimout(new function(){ submitter(form); }, 1000 );
   }
   else
  {
     form.submit();
     clearTimeout(timer);
   }
 }

答案 1 :(得分:0)

试试这个,

var ticks = 0;
var timer=null;
$("#form").validate({
    submitHandler: function(form) {
        $("#form").hide();
        $('#form-inscription').append('<h2>Text</h2>');
        ticks = 0;
        submitter(); //this is your timer function 
    }
});

function submitter()
{
  if(ticks < 10)
  {
    ticks++;
    timer = setTimout("submitter()", 1000 );
   }
   else
  {
     $("#form").submit();
     clearTimeout(timer);
   }
 }