开发者

While (do a jQuery) with a setTimeout or setInterval

I want to do the jQuery POST every 5 seconds with different POST values fro开发者_JAVA百科m a RS. The following code works but the function waits 5 seconds and sends all the data in the POST at once, instead of sending one POST, wait 5 sec, send next POST, wait 5 sec, etc.. I've been struggling with this for a week. I tried a setinterval and also an array, but nothing. Any help will be highly appreciated.

  <% while ((Repeat1__numRows-- != 0) && (!varmin.EOF)) { %>
    <script type="text/javascript">
        $(document).ready(function() {
                var phoneval = "<%=(varmin.Fields.Item("phone").Value)%>" 
                var smsval = "<%=checkinactive.Fields.Item("audio").Value%>"
                setTimeout(function(){
                $.post("Trigger.aspx", { phone: phoneval,
                sms: smsval }, function(data) {
                    $("#status p").html(data);
                });
                },5000);
                return false;
        });
    </script>
    <% Repeat1__index++; varmin.MoveNext(); } %>


Something along these lines should work, but I'm still questioning the point of this script.

<script type="text/javascript">
    jQuery(function ($) {
        var values = <%=
            /** 
             * Produce JSON formatted array on the server, like:
             * [{ phone : 123456, sms : 7891011 }, { phone : ... }, ...]
             **/
         %>;

         var i = 0;
         var interval = setInterval(function () {
            if (!values[i]) {
                window.clearInterval(interval);
                return false;
            }

            $.post("Trigger.aspx", values[i++], function (data) {
                $("#status p").html(data);
            });
         }, 5000);
    });
</script>


I added a JSON array and worked!

<%
var tempOutput = "";

while ((Repeat1__numRows-- != 0) && (!varmin.EOF)) 
{
    var phoneVal = varmin.Fields.Item("phone").Value;
    var smsVal = checkinactive.Fields.Item("audio").Value;

    tempOutput += "{\"phone\":\"" + phoneVal + "\", \"sms\":\"" + smsVal + "\"},";

    Repeat1__index++;
    varmin.MoveNext();
}

%>

<script type="text/javascript"> 
    jQuery(function ($) { 
        var values = output;
        .....
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜