var s = '<span toster="two">text</span>';
var el = document.createElement('div');
el.innerHTML = s;
var span = el.children[0];
var value;
var attrs = span.attributes;
for(let i = 0; i < attrs.length; i++) {
if( attrs[i].name === 'toster') {
value = attrs[i].value;
break;
}
}
if(value) {
// нашлось
}
var $script = require("scriptjs");
$script("//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js", function() {
$('body').html('It works!')
});
$({ myProp: A }) // начальное значение A
.animate(
{ myProp: B }, // целевое конечное значение B
{ // всякие опции анимации: время, функция на каждый шаг и т.п.
step: function(now, tween) { } // эта будет вызываться на каждом шаге анимации
}
);
const $price = $("#banner-message > p > span");
$("button").on("click", function(){
const oldPrice = parseInt($price.text());
const newPrice = Math.round(500 + Math.random() * 4500);
$({price:oldPrice}).animate({price:newPrice}, {
step: (now, tween) => $price.text(Math.round(tween.elem.price))
});
});
watch
это массив объектов, у каждого два свойства: HTML элемент и объект Date когда истекает срок.watch
:const Clock = {
watch: [],
add: function(el, D) {
this.watch.push({
el: el,
D : D,
});
this.tick();
},
tick: function(){
const now = new Date().getTime();
for(let i = this.watch.length - 1; i>=0; i--) {
let w = this.watch[i];
let dt = w.D.getTime() - now;
w.el.innerText = Math.round(dt / 1000);
if( dt<=0 ) {
w.el.classList.add('finished');
this.watch.splice(i,1);
}
}
},
timer: null,
init: function() {
this.timer = window.setInterval(this.tick.bind(this), 1000);
}
};
Clock.init();
Clock.add(
document.getElementById('obj1'),
new Date( new Date().getTime() + 5e3)
);
Clock.add(
document.getElementById('obj2'),
new Date( new Date().getTime() + 7e3)
);
Clock.add(
document.getElementById('obj3'),
new Date( new Date().getTime() + 3e3)
);
break;
сразу после founded = true;
и для скорости можно закэшировать текущие элементы arr[j]
и params[i]
для чуть более быстрого обращения к ним в цикле:_arr = arr[j];
// ...
_param = parametres[i];
if( _arr.p1 === _param.p1 && _arr.p2 === _param.p2) { ...
apc_store()
, apc_fetch()
и apc_inc() – увеличить сохранённое число. Object.assign()
работает так же, как var t1 = {};
t1.inbox = Terminal.inbox; // там массив, он по ссылке передаётся
t1.sendMail = Terminal.sendMail; // функция - тоже по ссылке, тоже будет === с t2
slice()
, чтобы получить независимую копию. var a = [1,2,3];
var b = a;
b.pop(); // a === b; a: [1,2];
// plan-b:
var a = [1,2,3];
var b = a.slice();
b.pop(); // b !== a; a: [1,2,3]; b: [1,2]
function factory(props) {
var Terminal = {
inbox: [],
sendMail: function (target, message) {
target.inbox.push({ terminal: this.name, message });
}
};
return Object.assign(Terminal, props);
}
var t1 = factory({ name: 'Jack' });
var t2 = factory({ name: 'Alice' });