我这里有这样的代码。我想以几秒钟的时间间隔显示结果。但这是行不通的。
const moveCranes = setInterval(function(plate, slot_a, slot_b, slot_c) {
if (plate > 0) {
moveCranes(plate - 1, slot_a, slot_c, slot_b);
port_a.innerHTML = "Move plate " + plate + " from " + slot_a + " to " + slot_c + "<br />";
moveCranes(plate - 1,slot_b, slot_a, slot_c);
}
}, 2000);
moveCranes(input.value,"slot_a","slot_b","slot_c");
您能解释我做错了什么吗,为什么setInterval不起作用吗?
最佳答案
您似乎在寻找
function moveCranes(plate, slot_a, slot_b, slot_c) { /*
^^^^^^^^ */
setInterval(function() {
// ^^^^^^^^^^ this inner function takes no parameters
if (plate > 0) {
moveCranes(plate - 1, slot_a, slot_c, slot_b);
port_a.innerHTML = "Move plate " + plate + " from " + slot_a + " to " + slot_c + "<br />";
moveCranes(plate - 1, slot_b, slot_a, slot_c);
}
}, 2000);
}
并且由于您要再次递归调用该函数,因此您可能想使用
setTimeout
而不是setInterval
。