如何停止“setInterval”并使用相同的FUNCTION启动另一个
How to stop a "setInterval" and start a other with the same FUNCTION
我真的需要你的帮助。我和不同的房间聊天。当用户单击"房间"按钮时,它会显示该房间上的消息,并每 2 秒更新一次 SetINTERVAL。
示例代码:
var abc = setInterval(function()
{
$.ajax({
type:"post",
url:"logs.php?user_profil=<?php echo $user_profil;?>&user=<?php echo $user;?>",
datatype:"html",
success:function(data)
{
$("#chatlogs").html(data);
}
});
}, 2000);
问题是,如果嗨点击另一个房间按钮,聊天会重叠。我怎样才能停止
VAR ABC 并用不同的值重新开始?
这是一个示例(这只是一个示例页面):
var myMethod = function() {
$.ajax({
type: "post",
url: "logs.php?user_profil=<?php echo $user_profil;?>&user=<?php echo $user;?>",
datatype: "html",
success: function(data) {
$("#chatlogs").html(data);
}
});
},
abc = setInterval(myMethod, 2000);
clearInterval(abc);
abc = setInterval(myMethod, 2000);
说明:将回调方法保存到变量中,并将该变量重用于 setInterval;
如何停止 VAR ABC 并使用不同的值重新启动它
顺便说一下,abc
变量只保存该特定间隔的 id。你真的不需要(也不能)"重新开始",你需要:)开始另一个。
我会这样做:
var getRoomUpdater = (function(){
var _userProfileId;
var updater = function() {
$.ajax({
type: "post",
url: "logs.php?user_profil=" + _userProfileId + "&user=<?php echo $user;?>",
datatype: "html",
success: function(data) {
$("#chatlogs").html(data);
}
});
}
return function(userProfileId){
_userProfileId = userProfileId;
return updater;
};
})();
// it will hold interval id
var abc;
// Opening a room:
var openRoom = function(roomId){
// start automatic update
clearInterval(abc);
abc = setInterval(getRoomUpdater(roomId));
// + other actions if needed
}
相关文章:
- 当包含另一个asp文件时,是否也包含所有引用的样式和脚本页面
- 使用clickToggle并在单击另一个元素时关闭元素
- 使用javascript将动态表从一个html页面打印到另一个html页
- 如何在android中使用phonegap将文件从一个文件夹移动/复制到另一个文件夹
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- 从Rally获取一个特定的标记,以便计算另一个字段中的值
- 无法从jquery Mobile导航栏重定向到另一个页面
- 如何在JQuery函数中定义一个值,然后调用另一个函数并使用该值
- 是否可以将一个函数输入连接到另一个函数调用的文本
- 字符串在将其传递给另一个活动Android JavaScript时读取Null
- 如何在鼠标悬停时在另一个图像上滑动图像.
- 导航到特定事件的另一个变量页面
- 正在传递JSONP标头's数据参数到另一个文件中的AJAX请求
- 如何在react js中将值从一个组件发送到另一个组件
- 按我自己的类克隆另一个元素的内容和顺序
- 在另一个函数中使用变量this
- Jasmine测试工厂函数调用本地函数和另一个函数-获取错误:应该是间谍,但得到了function
- 为什么一个 $(document).ready(function() { 会阻止另一个运行
- 如何停止“setInterval”并使用相同的FUNCTION启动另一个