条件为的ClearTimeout不工作
ClearTimeout with condition is not working
有人知道为什么这不起作用吗?
http://jsfiddle.net/jonevar/2Z2NQ/5/
这是整个代码:
function ag_alert(message) {
event.preventDefault();
//SetTimeout in case didn't close manualy
var timer = setTimeout(cls_message, 5000),
cur_url = window.location.href;
//Check if its already on
if (! $('.ag_alert_wrapper').is(':visible') ) {
//Set the language
(cur_url.indexOf('/en/') >= 0) ? cls_txt = "close" : cls_txt = "閉じる" ;
$('<div class="ag_mess ag_alert_wrapper"></div><div class="ag_mess ag_alert_wrapper_close">'+ cls_txt +'</div>')
.prependTo('body');
$('.ag_alert_wrapper')
.append('<p>'+ message +'</p>')
.animate({top : 0}, 200, function() {
$('.ag_alert_wrapper_close')
.animate({top : 90}, 200)
.on({
mouseenter : function () {
$(this).animate({
top : 100
}, 200);
},
mouseleave : function () {
$(this).animate({
top : 90
}, 200);
},
click : function () {
cls_message();
}
});
});
//Setups ESC key to close message
$(document).keydown(function(e) {
if (e.keyCode === 27) {
cls_message();
}
});
} else {
//if Alert is already visible
$('.ag_alert_wrapper')
.children('p').html(message)
.end()
.effect("highlight", {
color : '#FF0'
}, 1000);
clearTimeout(timer);
}
}
function cls_message() {
$('.ag_mess').animate({
top : -200
}, 200, function () {
$('.ag_mess').remove();
});
}
这似乎运行良好,测试代码(使用jQuery,但这不会改变结论):
html
<div id="msgs"></div>
js
function other_function() {
$('#msgs').append('other ');
}
function do_something(data) {
var timer = setTimeout(other_function, 500);
if (data === "condition") {
$('#msgs').append('hi ');
} else {
$('#msgs').append('clearing ');
clearTimeout(timer);
}
}
do_something('yay');
do_something('condition');
将其输出到div:
clearing hi other
正如预期的那样。实例:
- http://jsfiddle.net/mCdxV/
希望这能有所帮助。
相关文章:
- Javascript:selenium Web驱动程序isDisplayed()不工作
- jQuery UI自动完成突然停止工作
- AngularJS UI路由器不能像ng路由器那样工作
- HTML5音频加载和播放获胜'我不能在iPad上工作
- JavaScript打印功能使日历停止工作
- Javascript.getHours()工作不正常
- 为什么这在IE中的工作方式与在Firefox中不同
- 视频HTML没有'无法在Internet Explorer 11上工作
- 扩展移相器按钮类不工作
- Firebase迁移-简单的Firebase.set没有'不再工作了——旧的还是新的
- 谷歌地图不是以HTML显示,而是在JS Fiddle上工作
- 正在尝试使用if和else添加类,但无法正常工作
- Jquery FadeIn FadeOut 只工作一次
- Foreach无法在Typescript中工作
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- 为什么不是't窗口.恢复正常工作吗?(javascript/jquery)
- 函数外部的 clearTimeout() 无法正常工作
- 再次单击按钮时clearTimeout未按预期工作
- 条件为的ClearTimeout不工作
- javascript clearTimeout不是't从子页面工作