1. SetTimeout and setInterval are both timers in JS. They can specify a delay before executing an operation. The difference is that setTimeout stops after executing an operation after a specified time, while setInterval can keep repeating.
function fun(){
  alert('hello');
}
setTimeout(fun,1000);// The argument is the function name
setTimeout('fun()'.1000);// The argument is a string
setInterval(fun,1000);
setInterval('fun(),1000');
Copy the code

In the above code, either setTimeout or setInterval can take no arguments when using a function name as a call handle, but can take arguments when using a string. For example: setTimeout (‘ fun (name) ‘, 1000);

  1. Instead of defining a separate function, put the function call directly inside a function, using the function name as the call handle.
function fun(name){
  alert('hello'+' '+name);
}
setTimeout (function(){
  fun('Tom');
},1000);// The argument is the function name
Copy the code

In the above code, the difference between setTimeout and setInterval is that setTimeout is delayed by one second to pop up ‘hello’, after which it will no longer run. SetInterval will pop ‘hello’ every second until clear is used to clear the timer syntax.