2016-07-27 78 views

回答

0

window.setTimeout(function() { 
 

 
if (confirm('Do you want continue?')) { 
 
    // Do some ajax? 
 
} else { 
 
    // Do nothing! 
 
} 
 

 
}, 5000);

0

在技术方面它被称为长轮询请求..它是这样的

function longPolling() 
{ 
    $.ajax({ 
     //..... your ajax configurations like url, type, dataType...etc 
     success: function(data){ 
      // your code for handle success response 
      setTimeout(function(){ 
        longPolling(); 
       },5000) // call again after 5 seconds 
     }, 
     error: function(xhr){ 
      // your code for handle error response 
      setTimeout(function(){ 
        longPolling(); 
       },5000) // call again after 5 seconds 
     } 
    }; 
}); 

longPolling();// call where ever you need 

我希望这对您有所帮助

0

使用的setTimeout()函数在javascript或jquery中

0

var time = 5000; 
 
setTimeout(function() { 
 
    if (confirm('Do you want continue?')) { 
 
    var root = 'http://jsonplaceholder.typicode.com'; 
 

 
    $.ajax({ 
 
     url: root + '/posts/1', 
 
     method: 'GET' 
 
    }).then(function(data) { 
 
     console.log(data); 
 
    }); 
 
    } else { 
 
    // Do nothing! 
 
    } 
 

 
}, time)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

0

我从你的问题理解什么是你问用户之前“您是否愿意继续或不”正确的Ajax调用将进行?

我相信你想确保用户想等待,如果ajax调用需要很长时间?

如果这是你只是设置一个标志的用户按“无”的情况下,只是中止Ajax调用

// Assign handlers immediately after making the request, 
 
// and remember the jqXHR object for this request 
 
var jqxhr = $.ajax("example.php") 
 
    .done(function() { 
 
    alert("success"); 
 
    }) 
 
    .fail(function() { 
 
    alert("error"); 
 
    }) 
 
}); 
 

 
//when user pressed no call the below function if the ajax is not completed 
 
jqxhr.abort()