设置超时&通过单击清除超时即可开始计时
我对 Javascript 和 jQuery 还很陌生,不太明白 settimeout
。
我想做的就是通过点击启动计时器。它将滑动一个页面到有输入电子邮件的地方。如果 20 秒内没有输入任何内容,我希望它执行某些操作。但如果他们确实输入了某些内容,我希望将 20 秒计时器重置为 20 秒onkeyup
。最后,如果提交了表单,我希望取消超时。
$('#button').click(function(){
$('#pane').scrollTo( '1000px', 500);
setTimeout(goback, 20000);
function goback() {
$('#pane').scrollTo ('0px', 500);
$('#input').keyup(function(){
clearTimeout(goback);
setTimeout(goback, 20000);
};
$('#submitemail').click(function(){
//form submit stuff
clearTimeout(goback);
});
你能指出我正确的方向吗?
var timer;
$('#button').click(function(){
$('#pane').scrollTo( '768px', 0 );
$('#emailinput').delay(1000).focus();
$('#slide2fadeout').css("display", "block").fadeOut();
timer = setTimeout(goback, 20000); //in 20 seconds start goback function
function goback(){
$('#like').scrollTo( '0px', 0 ); //scroll back to top
$('#emailinput').val(''); //clear any partial entry
};
$('#emailinput').keyup(function(){
clearTimeout(timer);
timer = setTimeout(goback, 20000);
};
});
这是我的实际代码...似乎无法使其正常工作。任何帮助表示赞赏。
I'm pretty new with Javascript and jQuery and can't quite figure out settimeout
.
What I'm trying to do it start a timer with a click. It will slide a page to where there is an input to enter an email. If, in 20 seconds, there is nothing typed, I want it to do something. But if they do enter something, I'd like the 20 second timer reset to 20 seconds onkeyup
. And finally, if the form is submitted, I want the timeout cancelled.
$('#button').click(function(){
$('#pane').scrollTo( '1000px', 500);
setTimeout(goback, 20000);
function goback() {
$('#pane').scrollTo ('0px', 500);
$('#input').keyup(function(){
clearTimeout(goback);
setTimeout(goback, 20000);
};
$('#submitemail').click(function(){
//form submit stuff
clearTimeout(goback);
});
Could you please point me in the right direction?
var timer;
$('#button').click(function(){
$('#pane').scrollTo( '768px', 0 );
$('#emailinput').delay(1000).focus();
$('#slide2fadeout').css("display", "block").fadeOut();
timer = setTimeout(goback, 20000); //in 20 seconds start goback function
function goback(){
$('#like').scrollTo( '0px', 0 ); //scroll back to top
$('#emailinput').val(''); //clear any partial entry
};
$('#emailinput').keyup(function(){
clearTimeout(timer);
timer = setTimeout(goback, 20000);
};
});
Here's my actual code... can't seem to get it working right. Any help appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将
setTimeout
的返回值保存在某处,以便稍后取消:You need to save the return value of
setTimeout
somewhere in order to cancel it later: