console
function debounce(){
let elem = document.getElementById("animate");
let pos = 0;
let POS = 350;
let posss = 0;
let run = false;
return function(){
if(!run){
setInterval(function(){
run = true;
if (pos == 350)
{
POS--;
elem.style.top = POS + "px";
elem.style.left = POS + "px";
if(POS == 0)
{
pos = 0;
POS = 350;
}
}else {
pos++;
elem.style.top = pos + "px";
elem.style.left = pos + "px";
}
}, 5);
}
}
}
const moveBtn = document.querySelector("#move");
moveBtn.addEventListener('click',debounce());
<!DOCTYPE html>
<html>
<style>
#container {
width: 400px;
height: 400px;
position: relative;
background: yellow;
}
#animate {
width: 50px;
height: 50px;
position: absolute;
background-color: red;
}
</style>
<body>
<p><button id="move">单击我</button></p>
<div id ="container">
<div id ="animate"></div>
</div>
</body>
</html>