当以不同的间隔调用时,如何使相同的函数执行代码的不同部分

Dek*_*eke 0 javascript

这甚至可能吗?我想以不同的间隔使用相同的功能.如何使函数在5000运行并执行函数中的某些代码部分.

这是一个给出一个想法的例子:

 var moto = function(){
   // at 5000 run this part of code
   console.log("did stuff when called at 5000 time interval")

   // at 6000 run this part of code
   console.log("did stuff when called at 6000 time interval")
 }

 window.setInterval(moto, 5000)
 window.setInterval(moto, 6000)
Run Code Online (Sandbox Code Playgroud)

epa*_*llo 5

所以通过调用传递一个参数.

 var moto = function( val){
   if(val===5000) {
     // at 5000 run this part of code
     console.log("did stuff when called at 5000 time interval");
   } else {
   // at 6000 run this part of code
     console.log("did stuff when called at 6000 time interval");
   }
 }

 window.setInterval(moto.bind(this, 5000), 5000);
 window.setInterval(moto.bind(this, 6000), 6000);
Run Code Online (Sandbox Code Playgroud)