好的,直截了当地说
我在页面上有一个文本框和其他一些东西,如果用户正在输入文本框,那么页面就不应该刷新,否则它应该在一定的间隔后刷新。
我搜了很多遍也找不到类似的东西
我对javascript很陌生
发布于 2013-09-20 17:10:46
下面是一个简单的例子。每3秒运行一次检查。如果没有输入任何内容,它将刷新,如果输入了某些内容,则需要等待3秒才能刷新。
http://jsfiddle.net/9ARrG/
HTML
<input onkeyup="resetTimer = true">
JS
resetTimer = false;
setInterval(function() {
if(!resetTimer) {
location.reload();
}
resetTimer = false;
}, 3000);
发布于 2013-09-20 17:08:31
给输入/文本区域一个id
<input id="textbox" />
// OR
<textarea id="textbox"></textarea>
然后,设置一个计时器来刷新页面。如果有变化,重置计时器。
var originalTimer = 15000; // here's the original time until page refreshes
var timer = originalTimer; // timer to track whether to refresh page
// now every 1 second, update the timer
setInterval(function() {
timer -= 1000; // timer has gone down 1 sec
// if timer is less than 0, refresh page
if (timer <= 0) window.location.reload();
},1000); // repeat every 1 second (1000 ms)
document.getElementById("textbox").onchange = function() {
// detect textbox changes, reset timer
timer = originalTimer;
}
发布于 2013-09-20 17:19:30
使用document.activeElement
属性有条件地确定哪些元素具有焦点。
function refreshPageUnlessFocusedOn (el) {
setInterval(function () {
if(el !== document.activeElement) {
document.location.reload();
}
}, 3000)
}
refreshPageUnlessFocusedOn(document.querySelector('textarea'));
查看小提琴手中的工作示例。
https://stackoverflow.com/questions/18922012
复制相似问题