【发布时间】:2019-12-10 04:40:09
【问题描述】:
我有一个 html 表,它通过 jQuery Ajax 从数据库中获取它的值。 像这样的
<div id="tableId"></div>
比java脚本:
function showUser(rowsToShow) {
request = $.get("showTableFunction.php", {
q: rowsToShow
});
request.done(function(response, textStatus, jqXHR) {
$("#tableId").html(response);
});
request.fail(function(jqXHR, textStatus, errorThrown) {
console.log("The following error occurred: " + textStatus, errorThrown);
});
}
该表有一个 oninput 函数,每次用户更改其中一个单元格的值时都会更新数据库。
function updateCell(data) {
var cellId= data.id;
var editValue = $(data).text();
requestEdit = $.post("editCellFunction.php", {
cellId: cellId,
editValue: editValue,
});
requestEdit.done(function(response, textStatus, jqXHR) {
console.log(textStatus);
});
requestEdit.fail(function(jqXHR, textStatus, errorThrown) {
console.log("The following error occurred: " + textStatus, errorThrown);
});
}
但是,这会导致网站负载过大。 所以我想添加一个延迟,只有在没有新输入的情况下,比如说 5 秒,它才会执行 Ajax 请求
如果能帮上忙,将不胜感激。
【问题讨论】:
-
Javascript 的
setTimeout( functionToCallAfterTimeout, timeoutInMs )应该适合你...
标签: javascript jquery html ajax