<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<input class="btn" type="button" value="点我">
<script src="js/jquery-3.1.1.min.js"></script>
<script>
var timer = null;
timer = setTimeout(function() {
_ajax();
}, 1000);
});
function _ajax() {
$.ajax({
url: '',
type: 'get',
data: '',
success: function (data) {
console.log(data);
}
});
}
</script>
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<input class="btn" type="button" value="点我">
<script src="js/jquery-3.1.1.min.js"></script>
<script>
var timer = null;
$('.btn').click(function() {
//定义一个延时函数,用户不断点击会取消前一次,只执行最后一次请求
timer && clearTimeout(timer);timer = setTimeout(function() {
_ajax();
}, 1000);
});
function _ajax() {
$.ajax({
url: '',
type: 'get',
data: '',
success: function (data) {
console.log(data);
}
});
}
</script>
</body>
</html>