Javascript 未捕获类型错误:无法读取属性';停止';未定义的
我使用的是Javascript 未捕获类型错误:无法读取属性';停止';未定义的,javascript,jquery,ecmascript-6,Javascript,Jquery,Ecmascript 6,我使用的是JS[ES6],我能够在移动中显示通知,单击该x图标,它会消失得很好,但如果我让它进入超时它会抛出类型错误 Notify.js?49da:72 Uncaught TypeError: Cannot read property 'stop' of undefined 这是我的类代码片段 class Notify { constructor() { this.html = ''; } showNotification(text = 'Someth
JS
[ES6
],我能够在移动中显示通知,单击该x
图标,它会消失得很好,但如果我让它进入超时
它会抛出类型错误
Notify.js?49da:72 Uncaught TypeError: Cannot read property 'stop' of undefined
这是我的类
代码片段
class Notify {
constructor() {
this.html = '';
}
showNotification(text = 'Something went wrong!', style = 'warning'){
//here I need to update this.html
this.html = $(`<div class="alert alert-${style} hide">${this.icon} ${text} </div>`);
//close on click
let vue = this;
$('<a>', {
text: '×',
class: 'button close',
style: 'padding-left: 10px;',
href: '#',
click: function (e) {
e.preventDefault();
vue.removeNotice();
}
}).prependTo(vue.html);
vue.container.prepend(vue.html);
vue.html.removeClass('hide').hide().fadeIn('slow');
var timer = setInterval(vue.removeNotice, vue.time);
$(vue.html).hover(function () {
clearInterval(timer);
}, function () {
timer = setInterval(vue.removeNotice, vue.time);
});
vue.html.on('click', function () {
clearInterval(timer);
vue.removeNotice()
});
}
removeNotice() {
console.dir(this.html); //this just prints empty string
this.html.stop().fadeOut('slow').remove() //this line throws error.
}
类通知{
构造函数(){
this.html='';
}
showNotification(文本='出错!',样式='警告'){
//这里我需要更新this.html
this.html=$(`${this.icon}${text}`);
//点击关闭
让vue=这个;
$('', {
正文:“×”,
类:“按钮关闭”,
样式:“左填充:10px;”,
href:“#”,
点击:功能(e){
e、 预防默认值();
vue.removeNotice();
}
}).prependTo(vue.html);
vue.container.prepend(vue.html);
removeClass('hide').hide().fadeIn('slow');
var timer=设置间隔(vue.removeNotice,vue.time);
$(vue.html).hover(函数(){
清除间隔(计时器);
},函数(){
计时器=设置间隔(vue.removeNotice,vue.time);
});
on('click',function(){
清除间隔(计时器);
vue.removeNotice()
});
}
removeNotice(){
console.dir(this.html);//这只是打印空字符串
this.html.stop().fadeOut('slow').remove()//此行抛出错误。
}
你知道我哪里做错了什么吗?
我正在学习这些东西。请指向正确的方向
您没有正确绑定
timer = setInterval(vue.removeNotice.bind(this), vue.time);
应该会的。非常感谢。我其实没有想到这个。JS很棘手:/