const obj = {
count: 0,
start() {
this.intId = setInterval(() => {
if (this.count < 5) {
this.count++;
console.log('count', this.count);
} else {
clearInterval(this.intId);
}
}, 1000);
}
};
obj.start();
var Cookie = {
_params: {
domain: '',
path: '/',
expires: '',
maxAge: -1,
sameSite: false,
secure: false
},
_lastCookie: '',
setDefaultParams (params) {
this._params = {...this._params, ...params};
},
getDefaultParams () {
return this._params;
},
getLastCookie () {
return this._lastCookie;
},
set (key, value, params={}) {
value = encodeURIComponent(value);
params = {...this._params, ...params};
if (params.expires) {
if (params.expires instanceof Date) {
params.expires = params.expires.toUTCString();
} else if (typeof params.expires === 'string') {
const date = new Date(params.expires);
params.expires = date.toUTCString();
}
}
let cookie = `${key}=${value};`;
cookie += params.path ? ` Path=${params.path};` : '';
cookie += params.domain ? ` Domain=${params.domain};` : '';
cookie += params.expires ? ` Expires=${params.expires};` : '';
cookie += params.maxAge > -1 ? ` Max-Age=${params.maxAge};` : '';
cookie += params.sameSite !== false ? ` SameSite=${params.sameSite};` : '';
cookie += params.secure === true ? ` Secure;` : '';
this._lastCookie = cookie;
document.cookie = cookie;
return true;
},
has (key) {
return this.getAll().hasOwnProperty(key);
},
get (key) {
return this.getAll()[key];
},
getAll () {
const cookies = {};
this.getString().split(';').forEach(val => {
const params = val.split('=').map(el => el.trim());
cookies[params[0]] = params[1];
});
return cookies;
},
getString () {
return document.cookie;
},
delete (key) {
if (!this.has(key)) return false;
return this.set(key, '', { expires: 'Thu, 01 Jan 1970 00:00:01 GMT', maxAge: 0 });
}
};
if (!Cookie.has('key')) {
if (confirm('Are you want to save a cookie?')) {
Cookie.set('key', true, { expires: '2022-12-01T00:00:00' });
}
} else {
if (confirm('Are you want to delete a cookie?')) {
Cookie.delete('key');
}
}
const diffDate = (date, date2) => {
try
{
date = date instanceof Date ? date : new Date(date);
date2 = date2 instanceof Date ? date2 : new Date(date2);
}
catch (e)
{
console.log(e);
}
return date < date2;
};
const rangeDays = 120;
const createDate = new Date('2022-06-29T15:00:00'); // Строка в формате <input type="datetime" />
const rangeDate = new Date(createDate.getTime() + (1000*60*60*24*rangeDays));
const choiceDate = new Date('2022-11-25T15:00:00');
if (!diffDate(createDate, choiceDate) || !diffDate(choiceDate, rangeDate)) {
console.log('Invalid choosen date.');
return;
}
console.log('[debug][dates]', {
createDate: createDate.toString(),
choiceDate: choiceDate.toString(),
rangeDate: rangeDate.toString()
});
window.addEventListener("scroll", (e) => {
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
const elem = document.getElementById("target");
if (elem?.offsetTop <= scrollTop) {
document.location.hash = elem.id;
} else {
document.location.hash = '';
}
}, false);