关于以下功能,我有两个问题。显然,这是关于聊天的。在chat()
函数中调用了不同的函数,一个是建立连接,一个是搜索(随机)聊天的人,一个是每秒获取消息。
function chat()
{
//Open connection
var openconnection=openConnection();
//Stop function if connection could not be established
if(openconnection==false) return;
//Search for someone to chat with
searchforcontact=searchForContact();
if(searchforcontact==false) return;
//Constantly get messages
setTimeout(function(){
setInterval(getMessages(),1000);
},2000);
}
function openConnection() {
//Establish connection to php script
$.ajax({
type: 'POST',
url: 'action/chat/openconnection.php',
success: function(connection) {
//Let user know that someone to chat with is searched for
$('#chatTextDiv').append('bla');
//Return that the connection was successfull
return true;
}
}).error(function() {
//Let user know that a connection could not be established
$('#chatTextDiv').append('bla');
//Return false
return false;
});
}
以下是我的问题:
chat()
1:如果无法建立连接,我会使用 return 来停止函数。然而,功能继续searchForContact()
,即使失败,仍然继续。怎么来的?
2:函数getMessages()
只运行一次,不知道为什么?仅供参考,我使用超时来提高可用性。