$.ajax({
type: 'POST',
url: m_notice_click.ajax_url,
data: dataMnot,
dataType: 'json',
success: function(data) {
if (!$('.-notification-box').length) {
$('body').append('<div class="-notification-box" style="top: 0px;"></div>');
}
let $html = $(data);
let name = calcMD5($html.find('.inline-block.-notification-text').text());
if (items[name]) {
startTimer(items[name]);
} else {
items[name] = $html.find('.-notification-timer');
$('.-notification-box').append($html);
startTimer(items[name]);
}
console.log(items[name]);
function startTimer(name) {
let $timer = name;
timer.stop();
$timer.css("width", "100%");
timer.animate({
width: 0
}, {
duration: 5000,
complete: function() {
$(this).closest(notTime).remove();
delete items[name];
}
});
}
}
});
$.ajax({
type: 'POST',
url: m_notice_click.ajax_url,
data: dataMnot,
dataType: 'json',
success: function(data) {
if (!$('.-notification-box').length) {
$('body').append('<div class="-notification-box" style="top: 0px;"></div>');
}
let $html = $(data);
let name = calcMD5($html.find('.inline-block.-notification-text').text());
items[name] = $html.find('.-notification-timer');
console.log(items[name]);
if (items[name]) {
startTimer(items[name]);
} else {
items[name] = 1;
$('.-notification-box').append($html);
startTimer($html.find('.-notification-timer'));
}
function startTimer(name) {
let $timer = name;
timer.stop();
$timer.css("width", "100%");
timer.animate({
width: 0
}, {
duration: 5000,
complete: function() {
$(this).closest(notTime).remove();
delete items[name];
}
});
}
}
});
var Item = function(name) {
this.name = name;
}
var items = [];
$('button').on('click', function() {
if (!$('.-notification-box').length) {
$('body').append('<div class="-notification-box"></div>');
}
let $html = $(data);
let name = calcMD5($html.find('.inline-block.-notification-text').text());
items[name] = new Item(name);
if (items[name]) {
startTimer(name);
} else {
$('.-notification-box').append($html);
startTimer(name);
}
function startTimer(name) {
let $timer = items[name];
$timer.stop();
$timer.css("width", "100%");
$timer.animate(
{width: 0},
{duration: 10000,
complete: function() {
$(this).closest('.notice__info').remove();
delete items[name];
console.log(`Удален ${name}`);
}
}
);
}
});
if (items[name]) {
// повторно
startTimer(name);
} else {
// вновь
items[name] = 1;
var out = `
<div class="notice__info">
<div class="notice__info-name">
${name}
<div class="notice__timer" data-name="${name}"></div>
</div>
<div class="notice__delete">X</div>
</div>`;
$('.notice').append(out);
startTimer(name);
let Item = function(name, timer) {
this.name = name;
this.timer = timer;
}
const items = [];
if (!$('.-notification-box').length) {
$('body').append('<div class="-notification-box"></div>');
}
let $html = $(data);
$('.-notification-box').append($html);
let name = calcMD5($html.find('.inline-block.-notification-text').text());
items[name] = new Item(name, $html.find('.-notification-timer'));
items[name] = $html.find('.-notification-timer');
function startTimer(name) {
let $timer = items[name].timer;
$timer.stop();
$timer.css("width", "100%");
$timer.animate(
{width: 0},
{duration: 10000,
complete: function() {
$(this).closest('.notice__info').remove();
delete items[name];
console.log(`Удален ${name}`);
}
}
);
}
<div class="-notification-box" style="top: 0px;"></div>
this.name = name;
data-product_id="370"
var Item = function(id, data) {
this.id = id;
this.data = data;
}
const item = new Item(id, data);