0

我尝试在此函数中使用无限循环,但是,当我设置该函数时,setInterval该函数不会重复。问题是当用户发送表单并且时间间隔过去之后。该功能停留在发送的消息上,而不是从头开始。请看我的JSBin

Javascript:

$(function(){

  function show_fb() {
    $("#feedback_box").show("slow");    
  }

  function hideAll() {
    $("#feedback_box").hide("slow");
  }

  $("#feedback_box #close").click(function() { // When the X is pressed
    $("#feedback_box").hide("slow"); // Hide the form
  });

  setInterval(show_fb,1000); // Show the form after 6 seconds , you can change 6000 to anything you like    

  $("#feedback_form").submit(function() { //When the form is submitted

    $('#feedback_form #fb_title').css('background-color','#4444FF').text('Sending...').slideDown(900); //show "Sending" message

    $.post("form_submit.php",{ mesaj:$('#mesaj').val(),rand:Math.random() } ,function(data) { //subimit the feedback via AJAX

        $('#feed_subm').attr("disabled", "false"); //disable the "SEND" button
        $('#feedback_form #fb_title').css('background-color','#C33').text('Thank You!').slideDown(900); //Shows a thank you message
        setTimeout(hideAll,2000); // Hide the form after 2 seconds

    });

  });

  return false; //Tells the code not to post the form physically

});

HTML:

</head>
<body>

    <div id="feedback_box"> 
        <form id="feedback_form"> 
            <span id="fb_title">Give us some feedback :)</span><span id="close">X</span> 
            <textarea id="mesaj"></textarea> 
            <input type="submit" value="Send" class="mysubm" id="feed_subm"/> 
        </form> 
    </div>

</body>
4

1 回答 1

0

你可以像这样在 jQuery 中重复一个函数:

Javascript:

$(document).ready(function() {

  // First we will create the function that will be repeated
  function repeat() {

    setTimeout(function() {

      // Enter your code that you want to repeat in here

      repeat(); // This is where the function repeats

    }, 1); // Where 1 is the delay (in milliseconds)

  }

  repeat(); // Here we are initially firing the function

});

在上面的示例中,我不明白为什么您需要不断重复一个函数,但是您的问题是如何在 jQuery 中重复一个函数?

我认为这意味着如何在 jQuery 中重复一个函数?

好吧,这就是你的答案。

于 2013-01-07T02:25:47.077 回答