我试图在客户端每秒递增一个值,使用jquery
这是做了什么:
<script type="text/javascript">
$(document).ready(function increment(){
$("#counter").text(parseInt($("#counter").text())+1);
setTimeout(increment(),1000)
})
</script>
Run Code Online (Sandbox Code Playgroud)
这没有按预期工作,我收到"太多的递归"错误.
有什么想法吗?
我试图最终让这个代码工作:
function Timer(callback, delay) {
var timerId, start, remaining = delay;
this.pause = function() {
window.clearTimeout(timerId);
remaining -= new Date() - start;
};
this.resume = function() {
start = new Date();
timerId = window.setTimeout(callback, remaining);
};
this.resume();
}
var timer;
function onEvent(){
timer = new Timer(anotherEvent(), 5000);
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用,所以我简化它以查看可能存在的问题,并将其归结为:
var timer;
function event(){
timer = window.setTimeout(anotherEvent(), 5000);
}
Run Code Online (Sandbox Code Playgroud)
它所做的只是另一个事件().
有任何想法吗?
我想让一个元素淡入,并在它消失之前在页面上停留7秒.我可以随时取消它.我定义了以下功能.但是当我打电话时info_timeout.setup(ele, 'some msg here'),电子元件刚刚消失并立即消失.
我错过了什么吗?
var info_timeout = {
show_info: function(){
this.ele.html(this.msg).fadeIn('normal');
this.id = setTimeout(this.hide_info(), 7000);
},
hide_info: function(){
console.log(this.ele, this.id);
this.ele.fadeOut('slow');
delete this.id;
},
setup: function(ele, msg) {
this.ele = ele;
this.msg = msg;
this.cancel();
this.show_info();
},
cancel: function() {
if(typeof this.timeoutID == "number") {
clearTimeout(this.id);
delete this.id;
}
}
Run Code Online (Sandbox Code Playgroud)
};
谢谢.
我想通过下面的函数传递id1,id2和id3.这很好用:
function doSomething(id1,id2,id3) {
$(id1).fadeIn('slow',.25);
$(id1).fadeIn('slow',.25);
$(id1).fadeIn('slow',.25);
};
Run Code Online (Sandbox Code Playgroud)
但这不起作用:
function doSomething(id1,id2,id3) {
setTimeout( " $(id1).fadeIn('slow',.25) ", 300);
setTimeout( " $(id1).fadeIn('slow',.25) ", 300);
setTimeout( " $(id1).fadeIn('slow',.25) ", 300);
};
Run Code Online (Sandbox Code Playgroud)
如何让第二个工作?我的想法是我需要在id周围加上一些标点符号.或者我可以在setTimeout括号内为函数设置变量.有任何想法吗?
我试图在用户单击按钮时连续向文本输入字段的值添加+1.
简化,我的JQuery代码是这样的:
$('#button').on({
mousedown : function () {
var value = $(this).val();
$(this).val(value + 1);
},
mouseup : function () {
//Some other stuff here
}
});
Run Code Online (Sandbox Code Playgroud)
每次用户单击按钮时都可以使用此功能.我想要的是如果用户按下按钮,则mousedown事件每隔0.2秒触发一次,直到他停止按下(并且鼠标事件触发).
我认为这应该以某种方式用setTimeout()完成,但如果有人告诉我如何,我会很高兴.谢谢.
我正在编写一个jQuery插件,只需按一下按钮即可操作输入字段的值.
到目前为止我所拥有的是通过单击按钮来控制值的能力,以及如果用户按下按钮则不断增加它的能力.简化,脚本是这样的:
var element = $('#test-input');
var interval;
$('#test-up-button').on({
mousedown : function(e) {
element.val(parseInt(element.val()) + 1);
//Wait 400ms, than do the interval
interval = window.setInterval(function() {
element.val(parseInt(element.val()) + 1);
}, 200);
e.preventDefault();
},
mouseup : function() {
window.clearInterval(interval);
}
});
Run Code Online (Sandbox Code Playgroud)
(您还可以在此处查看工作版本:http://jsfiddle.net/Husar/Hxhsh/#base)
但是,正如您在注释中看到的那样,我也希望当mousedown事件发生时,在值初始增加之后,间隔函数将延迟400ms,而不是执行.
因此,您按下按钮,值变为+ 1,您按住按钮一点,然后间隔开始滚动.
<div>我的页面上有一个每两分钟自动刷新一次的更新日志条目.当我第一次加载我的网页时,我调用以下函数.
function getLogs() {
var filter = $('#filter').val();
$.get("index-ajax.asp", { queryType: "getLogs", filter: filter,
uTime: new Date().getTime() },
function(data){
$("#logEntries").html(data);
window.setTimeout("getLogs()",120000);
});
}
Run Code Online (Sandbox Code Playgroud)
我知道上面的代码可能更干净window.setInterval(...);但我只是喜欢控制window.setTimeout(...);.
我的问题是,是否可以取消下一次超时执行?如果我更改过滤器,我想取消下一个超时,并立即调用该函数,这将重新安排超时功能.有没有更好的方法来实现这一结果?
请注意,上面的代码是在jQuery中.
1)有人可以说明setTimeout如何在执行线程方面工作.
考虑:
function foo() { alert('foo'); }
function bar() { alert('bar'); }
setTimeout(foo,1000);
bar();
Run Code Online (Sandbox Code Playgroud)
要么
function foo() { alert('foo'); setTimeout(foo,1000); }
function bar() { alert('bar'); }
setTimeout(foo,1000);
bar();
Run Code Online (Sandbox Code Playgroud)
要么
function foo() { alert('foo'); setTimeout(foo,1000); }
function bar() { /* an execution that runs with unknown time */ }
setTimeout(foo,1000);
bar();
Run Code Online (Sandbox Code Playgroud)
要么
function foo() { alert('foo'); setTimeout(foo,1000); }
function bar() { /* some ajax call that reply with unknown time */ }
setTimeout(foo,1000);
bar();
Run Code Online (Sandbox Code Playgroud)
要么
function foo() { alert('foo'); setTimeout(foo,1000); } …Run Code Online (Sandbox Code Playgroud) 如何clearTimeout这个功能?
这是我的代码,我试过但不行.
我能怎么做 ?
.................................................. .................................................. .................................................. .......
<form id="fid">
<input type="text" id="input_type" onkeypress="stop()" onkeyup="run()"/>
</form>
<p id="myplace"></p>
<script>
function run() {
$('#myplace').hide();
setTimeout(function(){
$.ajax
(
{
url: 'xx.php',
type: 'POST',
data: $('#fid').serialize(),
cache: false,
success: function (data) {
$('#myplace').show();
$('#myplace').html(data);
}
}
)
}, 3000);
}
function stop() {
clearTimeout();
}
</script>
Run Code Online (Sandbox Code Playgroud) 我有一个看起来像这样的超时功能:
setTimeout(this.logout, 1000);
Run Code Online (Sandbox Code Playgroud)
登录方式:
logout() {
this.auth_token = "";
this.loggedIn = false;
this.emitLogedInStatusChange();
}
isLoggedIn() {
return this.loggedIn;
}
private emitLogedInStatusChange() {
this.LoggedInStatusChangedEmitter.emit({value: this.loggedIn});
}
Run Code Online (Sandbox Code Playgroud)
其中事件发射器告诉主组件其中loggedIn的值在哪里更改.问题是这个..emitLogedInStatusChange(); 我收到错误信息:
this.emitLogedInStatusChange is not a function
Run Code Online (Sandbox Code Playgroud)
我的问题是如何在注销中正确调用此函数,以便setTimeout可以工作?
这就是我所说的:
map((res) => {
if (res.username === username) {
this.auth_token = res.access_token;
this.sessionId = res.SessionID;
this.loggedIn = true;
this.expires = res.expires_in;
setTimeout(this.logout, this.expires*1000);
this.emitLogedInStatusChange();
}
Run Code Online (Sandbox Code Playgroud) settimeout ×10
javascript ×8
jquery ×6
ajax ×1
angular ×1
clear ×1
cleartimeout ×1
mousedown ×1
mouseevent ×1
recursion ×1
setinterval ×1
timeout ×1
timer ×1
window ×1