2009-01-11 162 views
4

我正在使用JavaScript,jQuery和PHP。我如何限制JavaScript函數執行一次?防止多次執行JavaScript函數

我的MainJQuery文件有Ajax。執行Display.php的了一會兒:

.... 

$.ajax({ 
    type:'POST', 
    url: 'display.php', 
    data:'id='+id , 
    success: function(data){ 
     $("#response").html(data); 

     //Here Get_list should be execute only once. 
     get_list('get'); 

     // Display should execute as usual 
     display(); 
    }//success 
}); //Ajax 

....... 

get.Php文件中寫入一個值給JavaScript:

<?php 
    print "<script language='javascript'>"; 
    print " g_cost[g_cost.length]=new Array('STA-VES','East',4500);"; 
    print " g_cost[g_cost.length]=new Array('STA-CRF','West',5400);"; 
    print "</script>"; 
?> 

我的JavaScript函數從PHP以下值:

function get_list('get'){ 
    for(var i=0;i<g_cost.length;i++) 
     var temp = g_cost[i]; 

    var Name = temp[0]; 
    var direction = temp[1]; 
    var cost = temp[2]; 

    .. 
    some code 
    ... 
} 

function display(){ 
    for(var i=0;i<g_cost.length;i++) 
     alert(g_cost.length); 
    var temp = g_cost[i]; 
    alert(temp[0]); 
    alert(temp[1]); 
    alert(temp[2]); 
} 

是有可能限制在jQuery Ajax成功部分中執行JavaScript函數?

+0

[只能運行一次javascript函數]可能重複(http://stackoverflow.com/questions/23859916/run-javascript-function-only-once) – Basilevs 2016-01-08 08:15:30

回答

10

在jQuery中可以使用.one()功能綁定,將只執行一次的方法。例如,

$("#someAnchorId").one("click", function(){ 
    //Ajax method here 
}); 

請參閱jQuery one help topic

0

嘗試在成功調用後禁用Ajax觸發器(鏈接,按鈕等)。觸發的

例子:

<a id="ajax_trigger" href="#" onclick="yourAjaxCall(); return false;">Get List</a> 

...

success: function(data){ 
    $("#response").html(data); 
     //Here Get_list should be execute only once 
     get_list('get'); 

     // Some code to disable, or hide #ajax_trigger 
     // in other words, make it unclickable or disappear. 
     $("#ajax_trigger").css('display': 'none'); // IIRC 

     // Display should execute as usual 
     display(); 
}//success 
3

三個選項:

  1. 坐落在全球範圍內一個布爾標誌,將其成功運行後, ,並在運行前檢查它

  2. 成功運行後,禁用按鈕(或任何用於調用一次性方法的控件)

  3. 稍微偏好,但您也可以在服務器端進行驗證,即分別調用方法時間,但在服務器端進行驗證。最重要的是,這將確保服務器上的數據一致性。

+0

引入全局變量是一個壞主意。 – 2009-01-11 13:23:06

3

要創建一個只執行一次的函數:

get_list = (function(){ 
    var counter = 0; 
    return function(param1, param2, ...) { 
    if (counter > 0) { 
     return; 
    } 
    counter++; 

    // your normal function code here 
    }; 
})(); 

這幾乎是一樣的使用全局變量來跟蹤多少次被執行的功能,但不是全局變量我們創建一個只有內部函數才能看到的變量。之後,您將get_list作爲其他函數使用。

這或許可以被重構到一些在功能上prototye比較一般,所以它可以像這樣使用:

get_list = (function (param1, param2, ...) { 
    ... 
}).once(); 
1

最快的方法是一個額外的行添加到成功的聲明:

$.ajax({ 
    type:'POST', 
    url: 'display.php', 
    data:'id='+id , 
    success: function(data){ 
     if(!arguments.callee.stop){ arguments.callee.stop = true; }else{ return; } 

     $("#response").html(data); 
     //Here Get_list should be execute only once 
     get_list('get'); 

     // display should execute as usual 
     display(); 

    }//success 
}); //ajax 
+0

使用arguments.callee真的很聰明,謝謝! – 2009-03-16 09:12:33

5

你可以用一個空函數替換功能 - 這是本質上是相同的Rene Saarsoo」的解決方案,但看起來更好:

var get_list = function(param1, param2, ...) { 
    // your code here 
    get_list = function() {}; 
}; 
+1

在「//你的代碼在這裏」你留下的代碼將會運行一次,在你將代碼放到每次運行的子功能中。 – jmav 2010-10-14 13:22:47