将代码放入函数中,并在成功或完整处理程序中调用它:
function load() { setTimeout(function () { $.ajax({ url: "includes/handlechat.php", type: "GET", data: data, dataType: 'json', success: function (result) { $("#clog").empty(); $.each(result, function (rowKey, row) { $("#clog").append('<p ><h4>' + row.username + ':</h4>' + row.message_content + '</p>'); }); }, complete: load }); }, 1101);}load();您还可以使用IIFE避免在当前环境中创建另一个绑定:
(function load() { // setTimeout here}());


