<style> _body{background-color: #0c0;} #myProgress { width: 100%; height: 30px; position: relative; background-color: #ddd; } #myBar { background-color: #4CAF50; width: 10px; height: 30px; position: absolute; } #move { position: relative; top:10%; color:#fff; text-align:center; z-index:999; } </style> <div id="url"></div> <div id="myProgress"> <div id="myBar"></div> <div id="move"></div> </div> <p id="demo"></p> <br> <div id="counter"></div> <input type="button" value="开始同步" onclick="init();"/> <input type="button" value="点击开始" onclick="startTimer();"/> <input type="button" value="点击暂停" onclick="stopTimer();"/> <script language="javascript" type="text/javascript"> var count=0; var a=true; function init(){ setInterval(getJindu,1000);//每隔1秒执行函数getJindu } function getJindu(){ if(a){ document.getElementById("counter").innerHTML=count++; move(); getData(count); }else{ document.getElementById("counter").innerHTML=count; } } function stopTimer(){ a=false; } function startTimer(){ a=true; } function move() {//进度条 var elem = document.getElementById("myBar"); var width = 0; var id = setInterval(frame, 10); function frame() { if (width == 100) { clearInterval(id); } else { width++; elem.style.width = width + '%'; //console.log(width); document.getElementById("move").innerHTML = width + '%'; } } } function getData(count){//直接发送get请求 console.log(count); var url="http://www.spw8.cn/b/api/api.php?api=view&id="+count; document.getElementById("url").innerHTML= url; var xhr = new XMLHttpRequest(); xhr.open('GET',url,true); xhr.onload = function(){ //console.log(this.responseText); if(this.responseText.indexOf("更新") == -1 ){ console.log("无更新"); stopTimer(); } } xhr.send(); } </script>