如果代码处理时间超过30秒,则运行另一个行代码

时间:2018-04-09 01:30:54

标签: javascript php ajax

我正在使用Web Service为我的网站开发新的支付系统。

...
$RESULT = DO_WEBSERVICE();
// If I dont get the result after 30 seconds, I want to cancel it using function
DO_CANCEL_WEBSERVICE();

我该如何解决这个问题?我应该使用AJAX吗?

1 个答案:

答案 0 :(得分:0)

您可以通过具有客户端 setTimeout() 的计时器延迟检查代码以查看您是否获得了所需的响应。

在下面的例子中,我只使用了5秒(5000毫秒)的延迟。

setTimeout(function(){
  // Do whatever test(s) you need to after the delay has expired
  // Here, I'm testing the textbox to see if it has any input
  if(document.querySelector("input").value === ""){
    alert("You haven't entered anything after 5 seconds!");
  } else {
    alert("Thanks for inputting some data.");
  }
}, 5000);
<input>