我有主页:
的index.php :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru" lang="ru">
<head>
...
$(".get_ajax").on("click", function(){
$(".for_ajax").show();
$(".for_ajax_content").html('<div class="LK_Loader">'
+ '<img src="./images/loader.gif" title="wait answer.." alt="wait answer..." width="25">'
+ '</div>');
$.post('./func.php?cmd=get_ajax', function(data){
$(".WindowInGame .WindowInGameText").html(data);
});
});
$(".for_ajax_exit").on("click", function(){
$(".for_ajax_content").html('');
$(".for_ajax").hide();
ajax_script_end = true;
});
...
</head>
<body>
....
<button type="button" class="get_ajax">get_ajax</button>
<div class="for_ajax">
<div class="for_ajax_exit">X</div>
<div class="for_ajax_content"></div>
</div>
....
</body>
</html>
在 func.php 中我获取了内容文件 test.php :
<button class="test" type="button">test</button>
<script>
var can_start = true;
$("body").on("click", ".test",function(){
console.log('can_start = ' + can_start);
if(!can_start){
alert("wait - script working");
return false;
}
else{
can_start = false;
}
$.ajax({
type: "POST",
url: "./func.php?Cmd=StartScript",
data: {val:'start'},
success: function(msg){
var answer = jQuery.parseJSON(msg);
if(answer.error == '0'){
setTimeout(function(){
alert('script work end');
can_start = true;
}, 20000);
}
else{
alert(answer.answer);
}
}
});
return false;
});
</script>
Logik工作:
<button type="button" class="get_ajax">get_ajax</button>
。test.php
的主页上加载内容页面for_ajax_content
for_ajax_content
我点击按钮test
并发送ajax。can_start
设置true
并显示alert('script work end');
我觉得我有问题:
main.php
并加载内容test.php
for_ajax_exit
,清除for_ajax_content
并隐藏for_ajax
在步骤3之后,如果我在控制台中打开控制台并加载页面test.php
,请参阅:
can_start = true; //corret value
can_start = false; //not corret - why do I get this value immediately after load script?
如果我点击按钮test
,我会得到alert("wait - script working");
。点击提醒中的ok
后,脚本会发送到func.php?Cmd=StartScript
我认为加载的脚本是重复的(我是对还是没有?),但是当我从加载的内容(单击for_ajax_exit
)退出时,如何销毁加载的脚本(超时中的脚本)?
P.S。:加载内容中的脚本应该在此内容中。如果我写的东西不清楚,我会很乐意解释。