Javascript 单个PHP文件中的两个设置间隔函数不起作用
加载页面时,函数updateCatajax()和updateCatajax1()一起工作。我只想在某个时间间隔内调用一个函数。在一个函数中更改时间,如将2000更改为3000或您想要的Javascript 单个PHP文件中的两个设置间隔函数不起作用,javascript,jquery,ajax,Javascript,Jquery,Ajax,加载页面时,函数updateCatajax()和updateCatajax1()一起工作。我只想在某个时间间隔内调用一个函数。在一个函数中更改时间,如将2000更改为3000或您想要的 function updateChatAJAx1() { var ajaxRequest; // The variable that makes Ajax possible! ajaxRequest = new XMLHttpRequest(); // Create a fun
function updateChatAJAx1()
{
var ajaxRequest; // The variable that makes Ajax possible!
ajaxRequest = new XMLHttpRequest();
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
//The response
document.getElementById('content1').innerHTML = ajaxRequest.responseText;
}
}
ajaxRequest.open("GET", "open_ajax.php?mode=ref", true);
ajaxRequest.send(null);
}
$(function(){
setInterval(function () {
updateChatAJAx1(); // It is javascript function which request to ajax file.
}, 2000);
});
$(function(){
setInterval(function () {
updateChatAJAx(); // It is javascript function which request to ajax file.
}, 2000);
});
加载时请像这样尝试同时执行这两个操作
$(function(){
setInterval(function () {
updateChatAJAx();
updateChatAJAx1(); // It is javascript function which request to ajax file.
}, 2000);
});
然后为两个操作设置间隔
updateChatAJAx();
updateChatAJAx1();
更改标志以选择操作
var flag=0;
$(function(){
setInterval(function () {
if(flag==0)
{
updateChatAJAx();
}
else
{
updateChatAJAx1();
}
}, 2000);
});
及
如果希望在使用一种模式时运行一个函数,在使用另一种模式时运行另一个函数,则需要某种方法来确定运行的模式 例如:
flag=1 -> updateChatAJAx1();
群聊
私人聊天
$(“#组”)。在(“单击”,函数(){
setInterval(updateChatAJAx,2000年);
});
$(“#个人”)。在(“单击”,函数(){
setInterval(updateChatAJAx1,2000);
});
您的意思是要在一个函数执行后调用另一个函数吗?我希望在某种条件下调用这两个函数。可能吗?我不明白。什么情况?你能举个例子吗?我正在开发一个聊天应用程序,其中有两种模式:团体聊天和个人聊天。发送消息后,我希望自动刷新内容,因此我为两种模式创建了这两个函数。当我使用personal chat时,updateCatajax()只应调用一个set interval函数;当我使用group chat时,则只应调用updateCatajax1()的set interval函数。但是,当我运行代码时,两个函数同时运行。有什么建议可以克服这个问题吗?我希望在某些条件下调用这两个函数。这可能吗?我正在开发一个聊天应用程序,其中有两种模式:团体聊天和个人聊天。发送消息后,我希望自动刷新内容,因此我为两种模式创建了这两个函数。但是,当我运行代码时,两个函数同时运行。有什么建议可以解决这个问题吗?您想如何运行这两个函数
flag=1 -> updateChatAJAx1();
<button id="group">Group chat</button>
<button id="personal">Personal chat</button>
$("#group").on("click", function(){
setInterval(updateChatAJAx, 2000);
});
$("#personal").on("click", function(){
setInterval(updateChatAJAx1, 2000);
});