Javascript 使用setInvernal每隔几秒钟调用一个查询函数?
我敢肯定,这很简单——我是一个初学者,正在努力将setInterval集成到我的jQuery中。我现在有这个功能,它可以在点击两个图像时旋转它们。我希望这种情况自动发生(每隔几秒钟),但似乎找不到使用setInterval的正确方法 谁能给我指一下正确的方向吗?非常感谢你的帮助 菲利浦Javascript 使用setInvernal每隔几秒钟调用一个查询函数?,javascript,jquery,Javascript,Jquery,我敢肯定,这很简单——我是一个初学者,正在努力将setInterval集成到我的jQuery中。我现在有这个功能,它可以在点击两个图像时旋转它们。我希望这种情况自动发生(每隔几秒钟),但似乎找不到使用setInterval的正确方法 谁能给我指一下正确的方向吗?非常感谢你的帮助 菲利浦 $(document).ready(function(){ $("#spinitemholder1 .sponsorFlipphil1 img").click(function () { $(
$(document).ready(function(){
$("#spinitemholder1 .sponsorFlipphil1 img").click(function () {
$(this).animate({ "width": "0px", "margin-left": "135px" }, 500, function () {
$(this).parent().hide();
$(this).width(0);
$("#spinitemholder1 .sponsorFlipphil2 img").animate({ "width": "271px", "margin-left": "0px" });
$("#spinitemholder1 .sponsorFlipphil2").show();
});
});
$("#spinitemholder2 .sponsorFlipphil2 img").click(function () {
$(this).animate({ "width": "0px", "margin-left": "135px" }, 500, function () {
$(this).parent().hide();
$(this).width(0);
$("#spinitemholder1 .sponsorFlipphil1 img").animate({ "width": "271px", "margin-left": "0px" });
$("#spinitemholder1 .sponsorFlipphil1").show();
});
});
});
试着跑步
setInterval( function(){
$('#spinitemholder1 .sponsorFlipphil1 img, #spinitemholder2 .sponsorFlipphil2 img' ).click();
}, 2000);
看看它是否有效。(只是为了测试代码,这不是建议的方法)尝试以下方法:
var interval = setInterval(function () {
$("#spinitemholder1 .sponsorFlipphil1 img").trigger("click");
$("#spinitemholder2 .sponsorFlipphil2 img").trigger("click");
}, 5000);
你必须使用“触发器”。我建议在用户单击其中一个图像时重置间隔。一个简单的方法是:
var interval = 0;
function startNewInterval() {
clearInterval(interval); // clear previous one
interval = setInterval(function () { // start new interval
$("#spinitemholder1 .sponsorFlipphil1 img").trigger("click");
$("#spinitemholder2 .sponsorFlipphil2 img").trigger("click");
}, 5000);
}
$("#spinitemholder1 .sponsorFlipphil1 img").click(function () {
$(this).animate({ "width": "0px", "margin-left": "135px" }, 500, function () {
$(this).parent().hide();
$(this).width(0);
$("#spinitemholder1 .sponsorFlipphil2 img").animate({ "width": "271px", "margin-left": "0px" });
$("#spinitemholder1 .sponsorFlipphil2").show();
startNewInterval();
});
});
$("#spinitemholder2 .sponsorFlipphil2 img").click(function () {
$(this).animate({ "width": "0px", "margin-left": "135px" }, 500, function () {
$(this).parent().hide();
$(this).width(0);
$("#spinitemholder1 .sponsorFlipphil1 img").animate({ "width": "271px", "margin-left": "0px" });
$("#spinitemholder1 .sponsorFlipphil1").show();
startNewInterval();
});
});
startNewInterval();
它在没有设置间隔的情况下工作吗?因为这里有一个语法错误:
“#spinitemholder.sponsorfliphil1 img”
我想您在代码中留下了一段XSLT。如果这与手头的问题无关,请您删除它好吗?问题是什么?为什么您认为这是setInterval
的错误?谢谢您的回复。我已经删除了xslt,很抱歉。它只是用来创建图像的ID。另外,问题是当我尝试使用setInterval时,代码停止工作。我想知道将它添加到代码中的正确方法是什么。当我按原样使用代码时,它会工作。当使用setInterval
方法时,您的此
引用不是都无效吗?我的意思是,this
在单击中引用了所单击的元素,而this
在方法调用中(本质上是setInterval
)引用了它自己的方法?@用户,在执行问题中的代码后运行此命令。如果它不起作用,那么您的原始代码也不应该起作用。非常感谢,这真的很有帮助。工作完美。