我正在尝试开发幻灯片,幻灯片之间有暂停.所以我正在尝试使用setTimeout语句,如下所示.这是写入交换2.jpg为1.jpg,点击按钮暂停10秒.但现在确实有效.谁能帮我.谢谢.
<html>
<head>
<script type="text/javascript">
var t;
function swap(newImage) {
var fileName=newImage.toString()+".jpg"
document.slideshow.src=fileName
t=setTimeout("swap()",10000)
}
</script>
</head>
<body>
<img name="slideshow" src="1.jpg" width="450" height="335" />
<br /><br />
<input type="button" onClick="swap('2')" value="Change image" />
<br /><br />
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 问题:当我在函数中声明clearInterval()/ setInterval()或clearTimeout()/ setTimeout()时,它什么都不做.我必须重置它的原因是用户可以单击以重置计时器.
我尝试了什么:我想要一个函数每3秒执行一次(因此我使用setInterval()与setTimeout()),并在点击时重置该计时器.我已经尝试使用setTimeout(),在每次执行函数时都使用clearTimeout()和setTimeout().结果:执行一次.
以下代码与setInterval相同.结果:它循环,永不重置.
// Declare variables as global
var varTimerSpeed = 3000;
var varTimerInterval;
varTimerInterval = setInterval("someFunction()", varTimerSpeed);
function someFunction() {
// Reset timer
clearInterval(varTimerInterval);
varTimerInterval = setInterval("someFunction()", varTimerSpeed);
}
// Call the function
$(".class").live("click",function() { someFunction(); });
Run Code Online (Sandbox Code Playgroud)
我在someFunction()中正在执行其他正确执行的事情,因此我知道click的处理程序正在运行.
我的非常简单的循环虽然不起作用,但似乎问题来自setTimeout.我尝试用倒计时编写一个小游戏.
while ( there is time ) {
// actions possible to do
//my timer function
}
Run Code Online (Sandbox Code Playgroud)
所以,如果c是时间
var c = 30;
while (c > 0)
{
c -= 1;
setTimeout( $("#timer").html(c) , 1000);
};
Run Code Online (Sandbox Code Playgroud)
它不仅仅是倒计时,而且用户必须能够在倒计时期间采取行动.谢谢你的帮助 !
这是我的代码:
function transition(index){
$("#right-panel").find("img").each(function(){
if ($(this).index() === index){
setTimeout(function(){
$(this).fadeIn(750);
}, 100000000);
}else{
$(this).fadeOut(750);
}
});
}
Run Code Online (Sandbox Code Playgroud)
由于某种原因,函数中的setTimeout不会导致fadeIn延迟.我究竟做错了什么?
我正在尝试传递元素索引并使用延迟滑动每个列表项内容
这是我的代码
for(var i = 1; i <= $("#colContainer li").length ; i++) {
var el = $("#colContainer li:nth-child(" + i + ") .colContent");
var delay = function() {
slide(el);
};
setTimeout(delay, 10);
function slide(el){
el.slideUp();
};
};
Run Code Online (Sandbox Code Playgroud)
但每次只是最后一个滑起来
我期望他们从索引1滑到延迟结束
我也尝试过这个
index = $(this).parent("li").index();
for(var i = 1; i <= $("#colContainer li").length ; i++) {
(function(i) {
var el = $("#colContainer li:nth-child(" + i + ") .colContent");
var delay = function() {
slide(el);
};
setTimeout(delay, 10);
function slide(el){
el.slideUp();
};
})(i); …
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建在将鼠标悬停在菜单名称上时下拉菜单.由于名称和菜单div不相邻,我需要一种方法来延迟菜单消失,因此用户可以从名称移动到菜单本身.我为此使用了setTimeout.一旦悬停在菜单上,我需要它保持打开,直到鼠标离开,之后它应该隐藏.
我试过的是一团糟.不知道如何解决它.因为setTimeout在初始鼠标悬停内,所以定时器会自行循环...但是如果把它放在其他任何地方,setTimeout似乎不起作用.
这是代码:
$(document).ready(function() {
$('.headermenushow').mouseover(function () {
$(this).next('.dropmenu').show(0, function () {
timer = setTimeout(function() {
$('.dropmenu').hide(10);
}, 2000);
});
$(this).next('.dropmenu').mouseover(function () {
clearTimeout(timer);
});
});
});
Run Code Online (Sandbox Code Playgroud)
这是一个简略的jsfiddle显示我想如何使用它:
任何帮助都会很棒.不确定如何让这个更好地工作......
我有一个带有setTimeout的函数.该函数的第一部分将超时设置为清除,以防它在setTimeout触发之前再次调用它.
clearTimeout不会清除计时器.我已经在调用了setTimeout的函数中的部分之后测试了clearTimeout(spinTimer)并且它可以工作.为什么一开始不起作用?
我已经全局声明了变量:
var spinTimer;
function cakeCubeSpin(whereTo){
clearTimeout(spinTimer);
... do some stuff
switch(whereTo){
case 1:
var spinTimer = setTimeout( function(){
$('#ccPanel4').css(leftReset);
$('#ccPanel2').css(rightReset);
$('#ccPanel3').css(backReset);
$('#ccPanel1').css(frontReset);
$('#cakeCubeWrapper').css(wrapReset);
$('#ccPanel1').addClass('cc-current');
}, ccSpeed);
break;
... more stuff
}
Run Code Online (Sandbox Code Playgroud) 我想将一个全局参数传递给a中的函数setTimeout
,但我希望该值在setTimeout
解释时保持不变:
var a=0;
setTimeout(function(){
console.log(a);
},5000,a)
a=1;
//output is 1 and should be 0
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?我已经搜索了Stack Overflow的答案,但没有找到任何答案.
我有以下代码涉及for循环中的IIFE(立即调用函数表达式).由于打印输出符合预期,因此IIFE功能显然可以获得正确的参数.但我不明白间隔在做什么.据我所知,第一次迭代的间隔应为1秒,然后第二次迭代的间隔为2秒等.
for (var i = 1; i <= 5; i++) {
(function(i){
setTimeout(function timer(){
console.log(i);
}, i*1000);
})(i);
}
Run Code Online (Sandbox Code Playgroud)
我看到i
以1秒为间隔打印出1..5.这对我来说很困惑,因为我期望每次迭代时间间隔都会增加.
使用以下代码:
for (var i = 1; i <= 5; i++) {
(function(i){
setTimeout(function timer(){
console.log(i);
}, 1000);
})(i);
}
Run Code Online (Sandbox Code Playgroud)
我看到i
在1秒间隔后立即打印所有值.
在setTimeout函数中发生了什么使它以观察的方式工作?
这是我正在检查页面是否空闲的html代码。如果闲置了一段时间,我需要抛出不活动警报并注销用户。
<div *ngIf="environmentData" xmlns="http://java.sun.com/jsf/html" (load)="startTimers()" (mousemove)="resetTimers()" (keypress)="resetTimers()">
Run Code Online (Sandbox Code Playgroud)
这些函数在打字稿组件中定义,如下所示:timoutNow变量设置为10分钟。但是,每当鼠标移动或按下某个键时,屏幕就会弹出警报。为什么在显示警报之前不等待我设置的那个时间?
// Start timers.
startTimers() {
console.log("inside start timers method");
console.log("time out number is "+this.timoutNow);
this.timeoutTimer = setTimeout(this.showTimeout(), this.timoutNow);
// window.location.href = this.environmentData.logoutUrl;
}
showTimeout(){
console.log("inside show timeout");
bootbox.alert("You are being timed out due to inactivity!");
}
// Reset timers.
resetTimers() {
console.log("inside reset timers method");
clearTimeout(this.timeoutTimer);
this.startTimers();
}
Run Code Online (Sandbox Code Playgroud) settimeout ×10
javascript ×8
jquery ×5
angular ×1
cleartimeout ×1
debugging ×1
setinterval ×1
typescript ×1
while-loop ×1