[英]How to use setTimeOut and setInterval in one function in javascript
Write a function customSetInterval (funcToExecute, interval) using recursion and setTimeout, which will repeat the functionality of the setInterval built-in method the setInterval built-in method takes two arguments: the first argument is a function that runs at a given time interval the第二個參數是以毫秒為單位的時間間隔。
function to call:
function executeMe () {
console.log ('123')
}
example of calling your function:
customSetInterval (executeMe, 1000)
as a result, the 123 line will be displayed in the console every second
從下面的評論中提取:
function customSetInterval(funcToExecute, interval) {
setTimeout(function() {
function executeMe() {
console.log('123');
customSetInterval();
}
}, 1000)
};
customSetInterval(funcToExecute, 1000);
我想這就是你想要的:
function executeMe() { console.log('123'); } function customSetInterval(fn, time) { setTimeout(function() { fn(); customSetInterval(fn, time) }, time); } customSetInterval (executeMe, 1000);
請試試這個:
function executeMe() {
console.log('123');
}
function customSetInterval (executeMe, second){
try {
setInterval(executeMe, second);
} catch (error) {
setTimeOut(executeMe, second);
}
}
customSetInterval("executeMe()", 1000);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.