jQuery自动刷新(setInterval) [英] JQuery auto refresh (setInterval)
问题描述
您好,人们不确定这样做的最佳方法-即将if
放在哪里.我有一个div加载页面并具有setInterval()函数.
Hi folks not sure the best way to do this - i.e. where to put the if
's .. I have a div that loads a page and has a setInterval() function.
在加载的页面上有1个按钮,我要实现的是单击按钮#1(加载的页面)以停止setInterval()并追加一个新的div(绝对位置),直到按钮#2(在页面上单击div),然后重新启动它. ..理解吗?
On the loaded page there is 1 button, What I want to acheive is when button #1 (loaded page) is clicked to stop the setInterval() and append a new div (position absolute) until button#2 (on the apended div) is clicked then to restart it. .. comprende?
这是我的基本"代码
这是第一个按钮的动作
$('.replybutton').live('click',function(){
$('.discussion').append('<div class="replyarea">some content in here plus "button number2</div>');
});
这将加载页面-最初是
$('.discussion').load('board2.php');
这是刷新功能
var auto_refresh = setInterval(
function()
{
$('.discussion').fadeOut().load('board2.php').fadeIn();
}, 10000);
任何操作失败-(但不是首选),我可以在加载的页面上使用切换按钮,而不是在$('.replybutton').live('click',function()
中使用的附加操作,但是仍然需要停止刷新并重新启动-基于切换条件,但是我强调切换的想法不是首选方式.
Failing anything - (but NOT preferred) I could use a toggle on the loaded page rather than the append used in the $('.replybutton').live('click',function()
, but would still need to stop the refresh and restart it - based on the toggle, but I stress the toggle idea is not the preferred way.
推荐答案
我在JSFiddle为您创建了一个示例.在此处查看: http://jsfiddle.net/7YYV7/.
I created an example for you at JSFiddle. Check it out here: http://jsfiddle.net/7YYV7/.
代码
var intervalId = 0;
intervalId = setInterval(fadeDiscussion, 3000);
$(function() {
$('input[name=click]').bind('click', function() {
clearInterval(intervalId);
$('.discussion').append('<div class="replyarea">some content in here plus <input type="button" name="save" value="save"></div>');
});
$('input[name=save]').live('click', function() {
intervalId = setInterval(fadeDiscussion, 3000);
});
});
var i = 1;
function fadeDiscussion () {
console.log(i);
$('.discussion').fadeOut().fadeIn();
i++;
}
这篇关于jQuery自动刷新(setInterval)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!