2016-12-27 1 views
-1

私はうまく動作するコードの次の部分がありますが、私は特定の条件が満たされたときに自動リフレッシュを停止したいと思います。特定の条件が満たされた後、AJAXコールでタイムアウト機能を停止する方法はありますか?

enter code here 
       $(document).ready(function() { 


    setTimeout(function(){ $.ajax({ 
    url: '/jrt/?Id=$data.jacket.id', 
    method: "GET", 
    cache: false, 
    success: function(data) { 
     //$("#gt").append(data); 
     $('#gt').html(data); 
    }, 
    error: function(jqXHR, textStatus, errorThrown) { 
     alert('error ' + textStatus + " " + errorThrown); 
    } 
}) 
    },10000); 


    }); 
+1

の可能性のある重複(http://stackoverflow.com/questions/8443151/how-to-stop-a-settimeout-loop) –

+0

のhttp:// WWW。 w3schools.com/jsref/met_win_clearinterval.asp – Nagaraju

答えて

0
$(document).ready(function() { 

var myVar; 

function yourFunction() { 
    myVar = setTimeout(function(){ 
     $.ajax({ 
      url: '/jrt/?Id=$data.jacket.id', 
      method: "GET", 
      cache: false, 
      success: function(data) { 
       $('#gt').html(data); 
      }, 
      error: function(jqXHR, textStatus, errorThrown) { 
       alert('error ' + textStatus + " " + errorThrown); 
      } 
     }) 
    },10000); 
} 

function stopYourFunction() { 
    clearTimeout(myVar); 
} }); 

使用あなたのsetTimeout関数を停止するには、てclearTimeoutを停止します。これを参照してくださいw3schools、良い説明があります

0

変数にsetTimeout関数を割り当て、任意の条件でクリアします。 [?のsetTimeoutループを停止する方法]

var timer; 

$(document).ready(function() { 
    timer = window.setTimeout(function() { 
    $.ajax({ 
     url: '/jrt/?Id=$data.jacket.id', 
     method: "GET", 
     cache: false, 
     success: function(data) { 
     //$("#gt").append(data); 

     $('#gt').html(data); 

     if (1) // your any condition 
     { 
      window.clearTimeout(timer); 
     } 
     }, 
     error: function(jqXHR, textStatus, errorThrown) { 
     alert('error ' + textStatus + " " + errorThrown); 
     } 
    }) 
    }, 10000); 
}); 
+0

ありがとうございました。 – DEO

関連する問題