settimeout_setinterval3.js

来自「源码JAVASCRIPT精粹」· JavaScript 代码 · 共 78 行

JS
78
字号
addLoadListener(init);var intervalProcess = setInterval("alert('GOAL!')", 3000);function init(){  var stopGoalLink = document.getElementById("stopGoalLink");  attachEventListener(stopGoalLink, "click", stopGoal, false);}function stopGoal(){  clearInterval(intervalProcess);}function addLoadListener(fn){  if (typeof window.addEventListener != 'undefined')  {    window.addEventListener('load', fn, false);  }  else if (typeof document.addEventListener != 'undefined')  {    document.addEventListener('load', fn, false);  }  else if (typeof window.attachEvent != 'undefined')  {    window.attachEvent('onload', fn);  }  else  {    var oldfn = window.onload;    if (typeof window.onload != 'function')    {      window.onload = fn;    }    else    {      window.onload = function()      {        oldfn();        fn();      };    }  }}function attachEventListener(target, eventType, functionRef, capture){  if (typeof target.addEventListener != "undefined")  {    target.addEventListener(eventType, functionRef, capture);  }  else if (typeof target.attachEvent != "undefined")  {    target.attachEvent("on" + eventType, functionRef);  }  else  {    eventType = "on" + eventType;    if (typeof target[eventType] == "function")    {      var oldListener = target[eventType];      target[eventType] = function()      {        oldListener();        return functionRef();      };    }    else    {      target[eventType] = functionRef;    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?