打字时不要刷新
这是我的代码:
// Refresh feeds, but ONLY when you are NOT typing
var refreshId = setInterval(function() {
$("#load-feeds").load('/actions/loadfeed.php?randval='+ Math.random());
}, 9000);
$.ajaxSetup({ cache: false });
我正在开发一种类似 Facebook 的“墙”,您可以在其中评论彼此的帖子。墙会使用 AJAX 自动刷新(每 9 秒),但是当您键入并且文本字段获得焦点时,它会在页面刷新后删除焦点和文本框的内容。
我怎样才能让它只在您不打字时刷新并在您打字时停止。谢谢!我到处寻找并尝试了一切,但没有运气。
This is my code:
// Refresh feeds, but ONLY when you are NOT typing
var refreshId = setInterval(function() {
$("#load-feeds").load('/actions/loadfeed.php?randval='+ Math.random());
}, 9000);
$.ajaxSetup({ cache: false });
I'm working on a kind of "wall" like Facebook where you can comment on each other's posts. The wall automatically refreshes with AJAX (every 9 seconds), but when you're typing and the textfield is focused it removes the focus and the content of the textbox after the page refreshed.
How can I make it only refresh when you're NOT typing and make it stop when you're typing. Thanks! I've looked everywhere and tried everything, but no luck.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想你有一个像这样的输入框:
你所要做的就是维护一个全局变量,而不是说这个输入是否具有焦点:
那么你只需要在允许更新之前检查这个变量:
I suppose you have an input box like this:
All you have to do is mantain a global variable than says if this input has the focus:
then you just have to check this variable before to allow the update:
您应该在他们键入时保持超时计时器运行,并在加载之前进行检查:
You should keep a
timeout
timer running when they type, and check before you load: