ven*_*lam 4 javascript php ajax jquery execution
我正在使用JavaScript,jQuery和PHP.如何限制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
.......
Run Code Online (Sandbox Code Playgroud)
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>";
?>
Run Code Online (Sandbox Code Playgroud)
我的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]);
}
Run Code Online (Sandbox Code Playgroud)
是否可以限制在jQuery Ajax成功部分中执行JavaScript函数?
red*_*are 10
在jQuery中,您可以使用该.one()
函数绑定一个只执行一次的方法.例如,
$("#someAnchorId").one("click", function(){
//Ajax method here
});
Run Code Online (Sandbox Code Playgroud)
请参阅jQuery一个帮助主题.
您可以使用空函数替换该函数 - 这与Rene Saarsoo的解决方案基本相同,但看起来更好:
var get_list = function(param1, param2, ...) {
// your code here
get_list = function() {};
};
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
12319 次 |
最近记录: |