js if和else if那里还能缩短吗,就是可以把这两段合并为一个吗,感觉挺多重复的
function updateNumber(el,option) {
let duration=option.duration;
let beforeNumber=option.beforeNumber;
let endNumber=option.endNumber;
let oneNumber=(endNumber-beforeNumber)/(duration*60);
let currentNumber=beforeNumber;
return function()
{
currentNumber+=oneNumber;
el.textContent=Math.floor(currentNumber);
if(oneNumber > 0)
{
if(currentNumber<endNumber)
{
if(currentNumber+oneNumber>endNumber)
oneNumber=endNumber-currentNumber;
requestAnimationFrame(arguments.callee);
}
}
else if(oneNumber < 0)
{
if(currentNumber>endNumber)
{
console.log(oneNumber);
if(currentNumber+oneNumber<endNumber)
oneNumber=endNumber-currentNumber;
requestAnimationFrame(arguments.callee);
}
}
}
}
回答:
function updateNumber(el, option) { const { duration, beforeNumber, endNumber } = option;
let oneNumber = (endNumber - beforeNumber) / (duration * 60);
let currentNumber = beforeNumber;
return function() {
currentNumber += oneNumber;
el.textContent = Math.floor(currentNumber);
if (oneNumber > 0 && currentNumber < endNumber) {
if (currentNumber + oneNumber > endNumber)
oneNumber = endNumber - currentNumber;
requestAnimationFrame(arguments.callee);
}
if (oneNumber < 0 && currentNumber > endNumber) {
if (currentNumber + oneNumber < endNumber)
oneNumber = endNumber - currentNumber;
requestAnimationFrame(arguments.callee);
}
};
}
回答:
function updateNumber(el, option) { let duration = option.duration;
let beforeNumber = option.beforeNumber;
let endNumber = option.endNumber;
let oneNumber = (endNumber - beforeNumber) / (duration * 60);
let currentNumber = beforeNumber;
return function() {
currentNumber += oneNumber;
el.textContent = Math.floor(currentNumber);
const rule =
oneNumber > 0 &&
currentNumber < endNumber &&
currentNumber + oneNumber > endNumber;
const rule1 =
oneNumber < 0 &&
currentNumber > endNumber &&
currentNumber + oneNumber < endNumber;
if (rule || rule1) {
oneNumber = endNumber - currentNumber;
requestAnimationFrame(arguments.callee);
}
};
}
回答:
if(oneNumber > 0 && currentNumber<endNumber && (currentNumber+oneNumber>endNumber)){
oneNumber=endNumber-currentNumber;
requestAnimationFrame(arguments.callee);
}else if (oneNumber < 0 && currentNumber>endNumber && (currentNumber+oneNumber<endNumber)){
oneNumber=endNumber-currentNumber;
requestAnimationFrame(arguments.callee);
}
以上是 js if和else if那里还能缩短吗,就是可以把这两段合并为一个吗,感觉挺多重复的 的全部内容, 来源链接: utcz.com/p/936643.html