function delay(timeout) {
return new Promise(res => setTimeout(res, timeout));
}
async function sendMessage(user, message) {
/* example */
console.log(user, message);
/* example */
}
async function sendAllTheMessagesToOurLovelyUsers(mailing_data, delay_between_requests) {
for (let mailing_entry of Object.values(mailing_data)) {
for (let user of mailing_entry.users) {
await sendMessage(user, mailing_entry.msg);
await delay(delay_between_requests);
}
}
};
sendAllTheMessagesToOurLovelyUsers([
{
users: [
{ user_id: 25 },
{ user_id: 14 },
],
msg: "message1"
},
{
users: [
{ user_id: 14 },
{ user_id: 6 }
],
msg: "message2"
}
], 500);
function delay(timeout) {
return new Promise(res => setTimeout(res, timeout));
}
async function sendMessage(user) {
/* example */
console.log(user);
/* example */
}
async function sendAllTheMessagesToOurLovelyUsers(user_lists, delay_between_requests) {
const users = {};
for (let user of user_lists.flat()) {
/* пример процедуры получения массива уникальных пользователей */
if (!(users[ user.user_id ] in users)) {
users[ user.user_id ] = user;
}
}
for (let user of Object.values(users)) {
await sendMessage(user);
await delay(delay_between_requests);
}
};
sendAllTheMessagesToOurLovelyUsers([
[
{ user_id: 25 },
{ user_id: 14 },
],
[
{ user_id: 14 },
{ user_id: 6 }
]
], 500);
(() => {
const re_template = /{(.*?)}/g;
String.prototype.process = function(data) {
return this.replace(re_template, (entry, word) => data[ word ]);
}
})();
let templates = {
greeting: 'Hello, my name is {name} and my age is {age}'
};
// Напрямую:
templates.greeting.process({ name: 'Yeshua', age: 2019 });
// Hello, my name is Yeshua and my age is 2019
templates.threat.process({ reason: 'i dont like you' });
// TypeError: Cannot read property 'process' of undefined
// Или автоматически вызывая process с помощью Proxy:
// (то же можно было реализовать просто с помощью дополнительной функции)
templates = new Proxy(templates, {
get(target, prop) {
if (prop in target) {
return String.prototype.process.bind(target[prop]);
} else {
throw new Error(`String "${ prop }" was not found in storage!`);
}
}
});
templates.greeting({ name: 'Yeshua', age: 2019 });
// Hello, my name is Yeshua and my age is 2019
templates.threat({ reason: 'i dont like you' });
// Error: String "threat" was not found in storage!
(() => {
var old_submit = HTMLFormElement.prototype.submit;
HTMLFormElement.prototype.submit = function() {
var form = this,
args = Array.prototype.slice.call(arguments),
submit_event = new Event('submit', {
bubbles: true,
cancelable: true
});
submit_event.original_submit = function() {
old_submit.apply(form, args);
};
form.dispatchEvent(submit_event);
}
})();
$(() => {
var my_form = $( 'form' )[0];
$( document )
.on('submit', 'form', function(e) {
alert('wtf?');
e.originalEvent.original_submit();
})
.on('click', 'button', function() {
my_form.submit();
});
})
function getWindowOf(element) {
return element.ownerDocument.defaultView || element.ownerDocument.parentWindow;
}
lst = ['text_1', 'text_2', 'text_3'];
interval = 2;
try {
item = lst[ Math.floor(time/interval) ];
} catch (e) {
item = value;
}
item