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 に答える