快速排序就是递归思想实现的。
1到n之间不能被2整除的数的积
function caculate(n) {
if (n === 1) return 1;
if (n % 2 === 0) return caculate(n -1);
return n * caculate(n - 1)
}
var total = caculate(10);
console.log(total)
1 - 100 之间不能被3整除的数之和
function excute(n) {
if (n === 1) return 1;
if (n % 3 === 0) return excute(n - 1);
return n + excute(n - 1)
}
var total = excute(100);
console.log(total)
通过setTimeout实现setInterval功能
利用递归调用自身,比setInterval更易控制
var timer = null;
var count = null;
function events() {
window.clearTimeout(timer); // 清除上一次定时器
count++;
console.log(count);
if (count === 3) { // 控制递归结束
console.log('结束');
return;
}
timer = setTimeout(events, 1000);
}