_btn.one('click', function(e){
var _html = _more.clone();
_html.hide().appendTo(_box).fadeIn(300);
console.log(_html);
});
_html.hide().appendTo(_box).slideDown(300);
$('.sounds-wrapper').on({
mouseenter: function() {
$(this).addClass('sounds-wrapper-active');
},
mouseleave: function() {
$(this).removeClass('sounds-wrapper-active');
}
});
$('#music-bottom').on('mouseenter', '.sounds-btn-left', function() {});
$('.sounds-btn-left', $('#music-bottom')).on('mouseenter', function() {});
$('#sound-bottom').on('hover', function() {
$(this).closest('.sounds-wrapper').addClass('sounds-btn-hover-right');
}, function() {
$(this).closest('.sounds-wrapper').removeClass('sounds-btn-hover-right');
});
cfsubmitText = $cfsubmit.text();
cfsubmitText = $cfsubmit.html();
$cfsubmit.text(cfsubmitText);
$cfsubmit.html(cfsubmitText);
var array = [{"id":"3","info":"my Info","startDate":"Fri Jun 08 2018 00:00:00 GMT+0200","endDate":"Thu Jun 21 2018 00:00:00 GMT+0200"},{"id":"4","info":"my Info 2","startDate":"Thu Aug 02 2018 00:00:00 GMT+0200","endDate":"Mon Aug 20 2018 00:00:00 GMT+0200"}];
for (var object in array) {
for (var key in array[object]) {
if (key === 'startDate' || key === 'endDate') {
array[object][key] = new Date(array[object][key]);
}
}
}
console.log(array);
var array = [{"id":"3","info":"my Info","startDate":"Fri Jun 08 2018 00:00:00 GMT+0200","endDate":"Thu Jun 21 2018 00:00:00 GMT+0200"},{"id":"4","info":"my Info 2","startDate":"Thu Aug 02 2018 00:00:00 GMT+0200","endDate":"Mon Aug 20 2018 00:00:00 GMT+0200"}];
changeArray(array, ['startDate', 'endDate'], function(value) {
return new Date(value);
});
function changeArray(array, keys, change) {
var find = function(key) {
if (typeof keys === 'string') {
return key === keys ? true : false;
} else if (Array.isArray(keys)) {
return keys.includes(key);
}
};
for (var object in array) {
for (var key in array[object]) {
if (find(key)) {
array[object][key] = change(array[object][key]);
}
}
}
return array;
}
$.extend({
// Определяем имя плагина
plugin: function(params) {
// Соединяем переданные настройки плагина с настройками по умолчанию
var setting = $.extend(true, {}, params, {
state: true,
key: 'prop'
});
// Основной код плагина
}
});
// Использование
$.plugin({
state: false
});
/**
* Установить к-во чисел после запятой (без округления)
* @param {number} number - число
* @param {number} fixed - к-во чисел после запятой, по умолчанию 4
* @returns {number} - фиксированное число
**/
function numberFixed(number, fixed) {
if ((typeof number === 'number' || typeof number === 'string') && !isNaN(number - parseFloat(number))) {
number = String(number);
var split = number.split('.');
if (split.length > 1) {
var left = split[0];
var right = split[1].substr(0, (!fixed ? 4 : fixed));
return Number(left + (fixed !== 0 ? '.' + right : ''));
} else {
return Number(number);
}
}
}
Number.prototype.customFixed = function(fixed) {
var number = this;
number = String(number);
var split = number.split('.');
if (split.length > 1) {
var left = split[0];
var right = split[1].substr(0, (!fixed ? 4 : fixed));
return Number(left + (fixed !== 0 ? '.' + right : ''));
} else {
return Number(number);
}
}
// Функция
numberFixed(515.6328456232, 5); //> 515.63284
// Метод Number.prototype
(515.6328456232).customFixed(5); //> 515.63284
function ajax(params) {
var request = new XMLHttpRequest();
request.open(params.method, params.url, params.async || true);
request.setRequestHeader('Content-Type', params.contentType || 'application/x-www-form-urlencoded; charset=UTF-8');
request.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status >= 200 && this.status < 400) {
if (params.success) params.success(this.responseText, this.statusText, this.status);
} else {
if (params.error) params.error(this);
}
} else {
if (params.error) params.error(this);
}
};
request.send(params.data ? JSON.stringify(params.data) : '');
request = null;
}
ajax({
url: 'ip.php',
method: 'POST',
contentType: 'application/json; charset=UTF-8',
data: {
x: 5,
y: 10
},
success: function(response, statusText, status) {
console.log('Запрос успешно отправился, получаем ответ', response);
},
error: function(XHR) {
console.log('Ошибка запроса', XHR);
}
});
<div class="controll">
<button type="button" data-value="all">All</button>
<button type="button" data-value="books">Books</button>
<button type="button" data-value="film">Film</button>
<button type="button" data-value="music">Music</button>
</div>
<div class="box">
<div class="item" data-value="books">
Books
</div>
<div class="item" data-value="film">
Film
</div>
<div class="item" data-value="music">
Music
</div>
<div class="item" data-value="books">
Books
</div>
<div class="item" data-value="film">
Film
</div>
</div>
(function($) {
'use strict';
$('button', $('.controll')).on('click', function(event) {
event.preventDefault();
var value = $(this).data('value');
if (value === 'all') {
$('.item', $('.box')).fadeIn(300);
} else {
$('.item[data-value!="'+ value +'"]', $('.box')).hide();
$('.item[data-value="'+ value +'"]', $('.box')).fadeIn(300);
}
});
})(jQuery);
Promise
, так как XHR выполняется асинхронно.function ajax(params) {
var request = new XMLHttpRequest();
request.open(params.method, params.url, params.async || true);
request.setRequestHeader('Content-Type', params.contentType || 'application/x-www-form-urlencoded; charset=UTF-8');
request.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status >= 200 && this.status < 400) {
if (params.success) params.success(this.responseText, this.statusText, this.status);
} else {
if (params.error) params.error(this);
}
} else {
if (params.error) params.error(this);
}
};
request.send(params.data ? JSON.stringify(params.data) : '');
request = null;
}
ajax({
url: 'lulz.json',
method: 'GET',
contentType: 'application/json; charset=UTF-8',
success: function(response, statusText, status) {
var mydata = JSON.parse(response);
console.log(mydata[0].name);
console.log(mydata[0].age);
console.log(mydata[1].name);
console.log(mydata[1].age);
},
error: function(XHR) {
console.log('Ajax Error', XHR);
}
});
function ajax(params) {
var request = new XMLHttpRequest();
request.open(params.type, params.url, params.async ? params.async : true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
// Успешный запрос
if (params.success) params.success(request.responseText);
} else {
// Запрос дошел до сервера, но вернул ошибку
if (params.error) params.error(request, request.status);
}
};
request.onerror = function() {
// Ошибка запроса
if (params.error) params.error(request, request.status);
};
request.setRequestHeader('Content-Type', params.contentType ? params.contentType : 'application/x-www-form-urlencoded; charset=UTF-8');
request.send(params.data);
}
ajax({
type: 'GET',
url: 'https://jsonplaceholder.typicode.com/posts',
contentType: 'application/json; charset=UTF-8',
data: { key: 'prop' },
async: true,
success: function(response) {
console.log(response);
},
error: function(xhr, status) {
console.log(xhr, status);
}
});
var timer = 0;
и в функции showtime(param)
перед вызовом setTimeout
чистить таймер clearTimeout(timer)
var timer = 0;
function showtime(param) {
var all=new Date();
var hours=all.getHours();
var minutes=all.getMinutes();
var seconds=all.getSeconds();
var timevalue = ((minutes<10) ? " :0" : " : ") + minutes;
timevalue += ((seconds<10) ? " :0" : " : ") + seconds;
if (param == 24) {
timevalue = " " + hours + timevalue;
}
else {
timevalue = " " + ((hours>12) ? hours-12 : hours) + timevalue;
timevalue += (hours>=12) ? " P.M. " : " A.M. ";
}
document.clock.next.value=timevalue;
clearTimeout(timer);
timer=setTimeout(function () {showtime(param);}, 1000);
}