0

私はこのコードを持っていますが、問題は、ループを停止してテキスト「Done!」を置き換えたいということです。これは、「textnew」文字列を含む sms-loader.php スクリプトから取得されます。問題は、ループがもう一度発生するため、div.checkstatus フィールドのテキストが呼び出し元の php スクリプトに再び置き換えられることです。

奇妙なことに、ログ メッセージが表示され、再び新しい (そして最終的な) 要求が返されますが、スクリプトでは順序が逆になっています (最初に停止してから text() を置き換える)。なぜこれが起こっているのかを理解する必要があります。

$(document).ready(function() {
var interval = "";

$('.checkstatus').each(function(){
    var msgid = $(this).data('msg')
        $this = $(this),
        hid = $this.data('history'),
        textnew = '<a href="sms-dstatus.php?id='+msgid+'&sid=' +hid+ '&amp;keepThis=true&amp;TB_iframe=true&amp;height=430&amp;width=770" title="Delivery Status" class="thickbox"><img src="../template/icons/supermini/chart_curve.png" alt="status" width="16" height="16" /></a>';

    interval = setInterval(function() {
    $this.load('../pages/sms-loader.php?id='+msgid);

        // stop the loop
        if($this.text()=='Done!'){
            // stop it
            clearInterval(interval);
            console.log(textnew);
            $this.html(textnew); /// this line is the problem
        }

    }, 5000); // 5 secs

});
});
4

3 に答える 3

0

$.ajax 関数の見栄えを良くするために「ヒント」に従いましたが、確実に制御できました。

$(document).ready(function() {
var interval = "";

$('.checkstatus').each(function(){
    var msgid = $(this).data('msg')
        $this = $(this),
        that = $this,
        hid = $this.data('history'),
        refreshimg = '<img src="../template/icons/smalls/10.png"/>';
    var interval = setInterval(function() {
    //$this.load('../pages/sms-loader.php?id='+msgid);
    $.ajax({
            url: "../pages/sms-loader.php",
            type: "GET",
            data:{id:+msgid},
            success: function(data) {
                if($this.text()=='Done!'){
                clearInterval(interval);
                }else{
                $this.html(data);
                }
              }

    }).done(function(){
        if($this.text()=='Done!'){
        console.log(refreshimg);
        $this.empty().html(refreshimg).show();
        clearInterval(interval);
        }
    });

    }, 5000); // 5 secs

});
});
于 2012-10-27T18:19:36.397 に答える
0

AJAX は非同期であるため、リクエストが行われている間、データが返される前に他のコードが起動します。目標を 100% 理解しているわけではありませんが、AJAX 成功コールバックの使用方法の概要に従ってください。

$this.load('../pages/sms-loader.php?id='+msgid, function(){

   /* AJAX complete and new html exists, run code here for after load-*/
   /* I believe you want to run clearInterval here */
});

の参照ドキュメントを参照してくださいload() : http://api.jquery.com/load/

于 2012-10-27T18:20:48.563 に答える
0

ローカルとしてマークするには、「interval」の前に「var」を付ける必要があります。

于 2012-10-27T16:58:31.753 に答える