2016-07-27 85 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()