0

My html page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><script src="js/jquery.js"></script>
<script>$(function(){
$("button:first").click(function(){
$("#aja").load("ajax.html");});
$("button:last").click(function(){
$("#aja").remove();});
});
</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<button>Load page</button><button id="rem">Remove ajax content</button>
<div id="aja"></div>
</body>
</html>

and ajax.html code

<body>
<script>$(function(){
cool();});
function cool(){
$("button:last").after("Hello");
setTimeout("cool()",10000);}
</script>
</body>

Now when i load ajax.html it cool() function runs and keeps on adding hello to last button .The problem is when i remove the ajax loaded content with second button,cool() function which is removed keeps on running .But i want the cool function to stop after removal of ajax content

4

2 回答 2

0

检查#aja块的存在可能会有所帮助:

function cool() {
    if ($("#aja").length == 0) return;

    $("button:last").after("Hello");
    setTimeout("cool()", 10000);
}
于 2012-05-22T17:44:33.833 回答
0

您想使用清除超时

http://www.w3schools.com/jsref/met_win_cleartimeout.asp

于 2012-05-22T17:45:38.550 回答