jquery - Run ajax for a fixed time if proper response is not received -


function submitform(){     var urlid = document.getelementbyid("actionurl").value;     jquery.support.cors = true;      settimeout(function(){       $.ajax({         url:urlid,         type : "get",         crossdomain : true,         data:transid,         success:function(data)         {             alert(data);         },          error: function (xhr, ajaxoptions, thrownerror)         {             console.log(thrownerror);             alert(xhr.status);             alert(ajaxoptions);             alert(thrownerror);         }         });     },1000); }; 

i have ajax call receives 1 among 3 responses 'true', 'false' or 'pending' controller. ajax should terminate if response 'true' or 'false' . if response 'pending' ajax should poll controller 20 seconds. should able pass response different javascript function. possible in ajax/jquery ? new technology.

basically extending michael's answer

function checkdata (data) {   if (data == 'true')         //   else if (data == 'false')   // else   else if (data == 'pending') {     if ( !pollserver.stoppoll ) {       ajaxpoll = settimeout(pollserver, 1000);     } else {       // polled long enough     }   } }  function pollserver() {   $.ajax({     url:         pollserver.urlid,     type:        "get",     crossdomain: true,     data:        transid,     success:     checkdata     error:       // error function   }); }  function submitform(){   var urlid = document.getelementbyid("actionurl").value;   jquery.support.cors = true;   pollserver.urlid = urlid;   pollserver.stoppoll = false;   ajaxpoll = settimeout(pollserver, 1000);   settimeout(function() {     pollserver.stoppoll = true;   }, 20000); } 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -