function startTimer(secs) {
var i = 0;
var timer = document.getElementById('timer');
var interval = setInterval(function() {
if (++i >= secs) {
clearInterval(interval);
timer.innerHTML = 'Done!';
} else {
++timer.innerHTML;
}
}, 1000);
}
startTimer(10);
Демо.function startTimer(secs) {
var timer = document.getElementById('timer');
setTimeout(function() {
timer.innerHTML = 'Done!';
}, secs * 1000);
}
startTimer(10);
Демо. function debounce(f, ms) {
let timer = null;
return function (...args) {
const onComplete = () => {
f.apply(this, args);
timer = null;
}
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(onComplete, ms);
};
}
function throttle(func, ms) {
var isThrottled = false,
savedArgs,
savedThis;
function wrapper() {
if (isThrottled) { // (2)
savedArgs = arguments;
savedThis = this;
return;
}
func.apply(this, arguments); // (1)
isThrottled = true;
setTimeout(function() {
isThrottled = false; // (3)
if (savedArgs) {
wrapper.apply(savedThis, savedArgs);
savedArgs = savedThis = null;
}
}, ms);
}
return wrapper;
}
function handler() {
// do something
}
var debouncedHandler = debounce(handler, 500);
document.querySelecror('.submit').addEventListener('click', debouncedHandler);
Unexpected token g in JSON at position 4
const json = JSON.stringify([
{
general: { /* ... */ },
/* ... */
},
/* ... */
]);
const comments = JSON.parse(json);
{
counterReducer: 0,
}
function mapStateToProps(state) {
return {
count: state.counterReducer,
};
}
const mapStateToProps = state => ({
count: state.counterReducer,
});