<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
#square {
width: 100px;
height: 100px;
border: 1px solid red;
transition: 0.05s;
}
</style>
</head>
<body>
<div id="square"></div>
<script>
var size = 100, flag = false, tm;
square.onclick = e => {
flag = !flag;
clearTimeout(tm);
(run = () => {
square.style.width = square.style.height = (flag ? size++ : size--) + 'px';
if (size > 0) tm = setTimeout(run, 50);
})();
};
</script>
</body>
</html>