(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); } } });