2013-02-13 232 views
0

另一个JavaScript函数,我有两个JavaScript函数呼叫时,功能不respondong

searchButtonClicked() 
hidemap() 

searchButtonClicked()将在body onload被调用。

searchButtonClicked()有时不回应。所以我想让它尝试执行5秒钟,如果它的剂量执行,我想打电话给hidemap()

请帮忙。提前致谢。 乌德

这里是功能

function searchButtonClicked() { 
    if (inlocatorHubMode) { 
    locatorHubSearch(); 
    } 
    else { 
    search(); 
    } 
} 
+0

searchButtonClicked'在内部执行什么操作?阿贾克斯? – dfsq 2013-02-13 11:48:07

+0

它从外部服务搜索数据并返回结果数组。 – 2013-02-13 11:48:52

+4

您应该了解为什么有时searchButtonClicked()不会响应,而是在失败时加载另一个函数。 – 2013-02-13 11:50:50

回答

1

我假设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请求。如果这对其他人有用,请留在这里。

+0

谢谢你的回答@Dolondro。但是我的函数没有Ajax调用。我将这个功能添加到了我的问题中。请看看它。再次感谢您的帮助。 – 2013-02-13 12:02:16

+0

我该如何设定等待时间?像5秒 – 2013-02-13 12:12:51

+0

糟糕,setTimeout应该读取'setTimeout(requestFailed,5000)'来表示它会持续5秒。编辑。 – Doug 2013-02-13 13:00:37

0

有以JavaScript

window.setTimeout("javascript function",milliseconds); 

此功能等待指定的毫秒数,然后执行指定的功能的功能。

在你的情况下,你可以在函数的开始处设置超时。 因此,在指定的毫秒后,您的第二个函数将执行