在下面的javascript函数window.setInterval(function,milliseconds)中,我可以执行以下操作:
var myvarible = function(){alert('Popup');};
window.setInterval(myvarible,5000);
现在我的网页每隔5秒就会提示“popup”消息
如何使我自己的函数执行参数myvarible,如上所示?这是我在下面尝试的,但它不起作用。
text = "";
var myvarible = function() {
for (i = 0; i < cars.length; i++) {
text += cars[i] + "<br>";
}
}
function howLong(the_function){
var starting_time = new Date().getMilliseconds();
the_function //do the function here
var finishing_time = new Date().getMilliseconds();
var difference = finishing_time-starting_time;
document.write(difference);
}
howLong(myVarible); //call my howLong function
我希望你理解我,我尽量让自己变得清晰,我是javascript的新手,所以请尽量让答案尽可能简单。
感谢您的帮助
答案 0 :(得分:2)
将()
放在变量后面,将其称为函数:
the_function(); // do the function here
答案 1 :(得分:0)
你必须进行3次更正。
1。)调用howLong(myVarible);
应为howLong(myvarible);
,因为您将函数定义为myvarible
2.)您必须在函数()
howLong
`the_function() //do the function here`
3。)定义cars
。
这对我有用
<script>
text = "";
var cars = [1,2,3];
var myVarible = function() {
for (i = 0; i < cars.length; i++) {
text += cars[i] + "<br>";
}
}
function howLong(the_function){
var starting_time = new Date().getMilliseconds();
the_function() //do the function here
var finishing_time = new Date().getMilliseconds();
var difference = finishing_time-starting_time;
document.write(difference);
}
howLong(myVarible); //call my howLong function
</script>