Ajax “輪詢”

$("#alreadypost").click(function () {

function getNowFormatDate() {

var date =new Date();

? ? ? ? var seperator1 ="-";

? ? ? ? var year = date.getFullYear();

? ? ? ? var month = date.getMonth() +1;

? ? ? ? var strDate = date.getDate();

? ? ? ? var hours=date.getHours();

? ? ? ? if(hours >=0 && hours <=9){

hours ="0" + hours;

? ? ? ? }

var minutes=date.getMinutes();

? ? ? ? if(minutes >=0 && minutes <=9){

minutes ="0" + minutes;

? ? ? ? }

var seconds=date.getSeconds();

? ? ? ? if(seconds >=0 && seconds <=9){

seconds="0" + seconds;

? ? ? ? }

if (month >=1 && month <=9) {

month ="0" + month;

? ? ? ? }

if (strDate >=0 && strDate <=9) {

strDate ="0" + strDate;

? ? ? ? }

var currentdate = year + seperator1 + month + seperator1 + strDate+" "+hours+":"+minutes+":"+seconds;

? ? ? ? return currentdate;

? ? }

var nums =0;

? ? var getting={

type:"post",

? ? ? ? url:"/data/searching",

? ? ? ? contentType:"application/json",

? ? ? ? dataType:"JSON",

? ? ? ? data:null,

? ? ? ? timeout:5000,

? ? ? ? success:function (data) {

nums=data.num;

? ? ? ? ? ? document.getElementById("alreadyget").innerText = nums;

? ? ? ? ? ? document.getElementById("alreadysend").innerText = data.data.sendNum;

? ? ? ? ? ? // document.getElementById("finaltime").innerText = data.msg;

? ? ? ? ? ? if(nums ==$("#times").val() || nums >$("#times").val()){

document.getElementById("finaltime").innerText=getNowFormatDate();

? ? ? ? ? ? ? ? window.clearInterval(stop);

? ? ? ? ? ? ? ? //ajax.abort();

? ? ? ? ? ? }

// alert("執(zhí)行了222");

// setTimeout(function(){$.ajax(getting);},5000)

? ? ? ? },

? ? ? ? error:function () {

alert("出錯");

? ? ? ? }

}

var ajax=function(){$.ajax(getting)};

? ? //window.setInterval(function(){$.ajax(getting)},5000);

? ? var stop=window.setInterval(function(){$.ajax(getting)},5000);

? ? // while(nums == $("#times").val()){

//? ? window.clearInterval(stop);

//? ? ajax.abort();

//? ? $("#alreadypost").click().abort();

// }

});

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容