var random = new Random(5632732);
console.log(random.number(0, 10)) //> 3.280606995884774
console.log(random.number(0, 10)) //> 3.679312414266118
console.log(random.boolean(50)) //> true
console.log(random.boolean(50)) //> true
function Random(seed) {
this.seed = seed;
}
Random.prototype.number = function(min, max) {
this.seed++;
return min + (((this.seed * 9301 + 49297) % 233280) / 233280) * (max - min);
};
Random.prototype.boolean = function(rate) {
this.seed++;
return Math.sin(this.seed) <= rate / 100;
};
var request = new XMLHttpRequest();
request.open('GET', 'https://jsonplaceholder.typicode.com/users', true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
var data = JSON.parse(request.responseText);
console.log(data);
} else {
// error
}
};
request.send();
$('.menu-open').on('click', debounce(function(event) {
console.log(this);
}, 500));
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this;
var args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
}
}
$('.timer').timer({
date: '12/25/2018',
language: 'RU',
template: function(time) {
return `
<span>
Осталось
${time.hours.value} ${time.hours.text},
${time.minutes.value} ${time.minutes.text} и
<span v-if="${time.seconds.value > 5}">
${time.seconds.value} ${time.seconds.text}
</span>
<span v-else style="color:red;">
${time.seconds.value} ${time.seconds.text}
</span>
</span>
`;
}
});
$('button').on('click', function() {
$(this).prop('disabled', true);
setTimeout(function() {
$(this).prop('disabled', false);
}.bind(this), 1e3);
});
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this;
var args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
$('button').on('click', debounce(function() {
console.log(this);
}, 500));
<div class="entry">
<h1>{{title}}</h1>
<div class="body">
{{body}}
</div>
</div>
<script>
template(document.querySelector('.entry'), {
title: 'Lorem ipsum dolor sit amet.',
body: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eos, optio.'
});
function template(node, data) {
node.innerHTML = tag(node.innerHTML, data);
}
function tag(input, data) {
for (var key in data) {
input = input.replace(new RegExp('{{'+ key +'}}', 'g'), data[key]);
}
return input;
}
</script>
function isNumeric(value) {
var type = typeof value;
return (type === 'number' || type === 'string') && !isNaN(value - parseFloat(value));
}
isNumeric('2562362') //> true
isNumeric('263621.621561sg') //> false
var html = `
<div class="single-question">
<span class="{{class}}">{{text}}</span>
</div>
`;
var result = tag(html, {
class: 'test',
text: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eaque, magni.'
});
function tag(input, data) {
for (var tag in data) {
input = input.replace(new RegExp('\\{{'+ tag +'\\}}', 'gi'), data[tag]);
}
return input;
}
<div class="single-question">
<span class="test">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eaque, magni.</span>
</div>