var timer; function hogehoge(){ timer = setTimeout(piyopiyo, 5000); var scriptB = document.createElement("script"); scriptB.src = "b.js?" + Math.random(); var head = document.getElementsByTagName("head")[0]; head.appendChild(scriptB); } function piyopiyo(){ alert("disconnect"); } function fugafuga(){ clearTimeout(timer); alert("connect"); }