如果我有 300 个这样的函数:
function name1() {
alert("1");
}
function name2() {
alert("2");
}
function name3() {
alert("3");
}
等等一直到 300,我如何将这些全部连接到一个函数中,因为它们非常相似,尽管每个函数确实有细微的差异?
更新:
抱歉,我使用了示例函数,因为我认为将长代码放入问题中并不重要,但我认为现在确实很重要,这里是真正的函数:
function event() {
//paths
var path = document.querySelector('#container #path');
var length = path.getTotalLength();
console.log(path.getTotalLength());
// Clear any previous transition
path.style.transition = path.style.WebkitTransition =
'none';
// Set up the starting positions
path.style.strokeDasharray = length + ' ' + length;
path.style.strokeDashoffset = length;
// Trigger a layout so styles are calculated & the browser
// picks up the starting position before animating
path.getBoundingClientRect();
// Define our transition
path.style.transition = path.style.WebkitTransition =
'stroke-dashoffset 0.4s linear';
// Go!
path.style.strokeDashoffset = '0';
document.getElementById("container").style.visibility = "visible";
};
function event2() {
//paths
var path2 = document.querySelector('#container2 #path_2');
var length = path2.getTotalLength();
console.log(path2.getTotalLength());
// Clear any previous transition
path2.style.transition = path2.style.WebkitTransition =
'none';
// Set up the starting positions
path2.style.strokeDasharray = length + ' ' + length;
path2.style.strokeDashoffset = length;
// Trigger a layout so styles are calculated & the browser
// picks up the starting position before animating
path2.getBoundingClientRect();
// Define our transition
path2.style.transition = path2.style.WebkitTransition =
'stroke-dashoffset 0.45s linear';
// Go!
path2.style.strokeDashoffset = '0';
document.getElementById("container2").style.visibility = "visible";
};
function event3() {
//paths
var path3 = document.querySelector('#container3 #path_3');
var length = path3.getTotalLength();
console.log(path3.getTotalLength());
// Clear any previous transition
path3.style.transition = path3.style.WebkitTransition =
'none';
// Set up the starting positions
path3.style.strokeDasharray = length + ' ' + length;
path3.style.strokeDashoffset = length;
// Trigger a layout so styles are calculated & the browser
// picks up the starting position before animating
path3.getBoundingClientRect();
// Define our transition
path3.style.transition = path3.style.WebkitTransition =
'stroke-dashoffset 0.4s linear';
// Go!
path3.style.strokeDashoffset = '0';
document.getElementById("container3").style.visibility = "visible";
};
window.setTimeout(function() {
event();
}, 1);
window.setTimeout(function() {
event2();
}, 550);
window.setTimeout(function() {
event3();
}, 1100);
所以我必须对它们进行更改,但大部分都是相同的,那么给定这段代码我该如何做呢?非常感谢。
最佳答案
创建一个带有参数的函数:
function name(myNumber) {
alert(myNumber);
}
name(1);
name(2);
name(3);
关于javascript - 连接 JavaScript 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26806771/