我假设 searchButtonClicked 是一个 AJAX 请求。
如果是这种情况,您可以使用 setTimeout 和回调的组合来实现您的目标。
var searchTimeout = null;
function searchButtonClicked(){
searchTimeout = setTimeout(requestFailed, 5000);
//If you're using jQuery, something like this
$.ajax({
url: "service.php",
success: requestSuccess
});
//If you're not, something like this
xmlhttp.onreadystatechange=(function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200){
requestSuccess();
}
});
}
function requestSuccess(){
clearTimeout(searchTimeout);
//Yay! It succeeded!
}
function requestFailed(){
hideMap();
//It didn't unfortunately =[
}
编辑:编辑说它实际上不是 AJAX 请求。把它留在这里,以防它对某人有用。