//设置定时器:5秒后关闭两侧的广告栏 setTimeout(fn,5000); function fn(){ imgArr[0].style.display = "none"; imgArr[1].style.display = "none"; } } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. setTimeout 的网红面试题 下方代码的执行结果是? AI检测代码解析 for (var i = 0; i < 10;...
setTimeout(fn,1000); function fn(){}; //还可以这样用 setTimeout(function(){},1000); //interval一样 1. 2. 3. 4. 5. 我们demo上设置的延迟时间是1000ms,由于js是单线程的,实际的延迟时间可能会比 1000ms长,但是只要进程空闲时就会立即执行。(这也是为什么建议使用setTimeout来代替setInterval的原...
setTimeout(o.fn, 1000);//执行后会打印出什么 错误思路:setTimeout执行,调用对象O的fn函数,由于调用者是对象O,那么this也指向了对象O,又对象O中有属性i,则会打印出1。 正解:因为setTimeout是window对象的方法,传入o.fn只是将o.fn这个函数传给了setTimeout,仍然是window对象在调用。上面代码执行的正确结果...
setTimeout 第三个及之后的参数作用:定时器启动时候,第三个以后的参数是作为第一个fn()的参数传进去。 1 2 3 4 5 6 // setTImeout 第三个及以后参数是作为第一个函数 fn 的参数传入 setTimeout(fn, 1000, 1, 2)<br> functionfn (x, y) { console.log(x, y) } // 输出 1,2...
constfn=MyObj.showNamesetTimeout(fn,1000) 这样看,在setTimeout里面,当执行到的时候,实际上就是在window下执行fn,此时的this,就指向了window,而不是原来的函数。 setTimeout 存在嵌套调用问题 如果setTimeout 存在嵌套调用,调用超过5次后,系统会设置最短执行时间间隔为 4 毫秒。 我们可以在浏览器粗略测试一下...
;},0);vara=10;console.log(b);console.log(fn);varb=20;functionfn(){setTimeout(function(){console.log('setTImeout 10ms.');},10);}fn.toString=function(){return30;}console.log(fn);setTimeout(function(){console.log('setTimeout 20ms.');},20);fn();...
(function(){vartimeouts=[];varmessageName="zero-timeout-message";functionsetZeroTimeout(fn){timeouts.push(fn);window.postMessage(messageName,"*");}functionhandleMessage(event){if(event.source==window&&event.data==messageName){event.stopPropagation();if(timeouts.length>0){varfn=timeouts...
window.onload = function () { //获取相关元素 var imgArr = document.getElementsByTagName("img"); //设置定时器:5秒后关闭两侧的广告栏 setTimeout(fn,5000); function fn(){ imgArr[0].style.display = "none"; imgArr[1].style.display = "none"; } } setTimeout 的网红面试题 下方代码的执...
let obj = {fn() {console.log(this) // obj// 示例1let timer1 = setTimeout(function() {console.log('我是timer1的this指向:', this) // Window}, 1000)// 示例2 (让定时器函数中的this是obj:使用变量保存的方式)let _this = thislet timer2 = setTimeout(function() {console.log('我是...
sendCode:function(e){varthat=this;vartimes=0vari=setInterval(function(){times++if(times>=60){that.setData({color:"#ff6f10",disabled:false,getCode:"获取验证码",})clearInterval(i)}else{that.setData({getCode:"重新获取"+times+"s",color:"#999",disabled:true})}},1000)} ...