JavaScript异步调用定时方法并停止该方法

(function() { 
    var t = window.setInterval(function() { 
        $('#ajaxGame').load('RefreshCurrentPlayerServlet #ajaxGame', function() { 
            youWin(); 
            youlose(); 
        }); 
    }, 10000); 
 
    function youWin()  { 
        if ($('#status:contains("YOU ARE THE WINNER!")').length) { 
            alert("YOU ARE THE WINNER!"); 
            clearInterval(t); 
        } 
    } 
 
    function youlose() { 
        if ($('#status:contains("You lost!")').length) { 
            alert("You lost!"); 
            clearInterval(t); 
        } 
    } 
});
posted @ 2012-03-15 21:11    阅读(606)  评论(0编辑  收藏  举报