无法在另一个函数内调用一个函数 javascript
Cannot call a function inside another function javascript
<script src="https://raw.githubusercontent.com/mckamey/countdownjs/master/countdown.js"></script>
<script> function getAge (date) { k = countdown(date); } </script>
它会抛出一个错误
countdown is not defined
更新
我将文件下载到本地,当我尝试直接调用时,它可以工作,但在函数内部时却没有。
这是因为 GitHub 提供的原始文件没有设置正确的 MIME 类型,因此浏览器出于安全原因不会下载它们。
相反,使用一些替代的慷慨的主机,这些主机确实以正确的 MIME 类型提供服务。
我常用的是githack.com
。要使用它,只需将 URL 中的 githubusercontent
更改为 githack
即可得到:
https://raw.githubusercontent.com/mckamey/countdownjs/master/countdown.js
我不明白 "countdown is not defined",我明白 "date is not defined"。现在我有机会查看 countdown.js
文件,您没有将所有参数发送到函数:
function countdown(start, end, units, max, digits) {
var callback;
// ensure some units or use defaults
units = +units || DEFAULTS;
// max must be positive
max = (max > 0) ? max : NaN;
//…
首先,您需要定义"date"或使用"new Date()"。其次,还有其他参数需要设置,包括回调和要更新的目标元素。我建议阅读文档 here。
这是自述文件中的示例:
var timerId =
countdown(
new Date(),
function(ts) {
document.getElementById('pageTimer').innerHTML = ts.toHTML("strong");
},
countdown.HOURS|countdown.MINUTES|countdown.SECONDS);
// later on this timer may be stopped
window.clearInterval(timerId);
最后,您实际上并没有在我能看到的任何地方调用 getAge 函数。你只是定义了它。如果你 运行:
getAge(new Date());
您不会收到任何错误,但它不会执行任何操作,因为您尚未定义任何其他参数。
<script src="https://raw.githubusercontent.com/mckamey/countdownjs/master/countdown.js"></script>
<script> function getAge (date) { k = countdown(date); } </script>
它会抛出一个错误
countdown is not defined
更新
我将文件下载到本地,当我尝试直接调用时,它可以工作,但在函数内部时却没有。
这是因为 GitHub 提供的原始文件没有设置正确的 MIME 类型,因此浏览器出于安全原因不会下载它们。
相反,使用一些替代的慷慨的主机,这些主机确实以正确的 MIME 类型提供服务。
我常用的是githack.com
。要使用它,只需将 URL 中的 githubusercontent
更改为 githack
即可得到:
https://raw.githubusercontent.com/mckamey/countdownjs/master/countdown.js
我不明白 "countdown is not defined",我明白 "date is not defined"。现在我有机会查看 countdown.js
文件,您没有将所有参数发送到函数:
function countdown(start, end, units, max, digits) {
var callback;
// ensure some units or use defaults
units = +units || DEFAULTS;
// max must be positive
max = (max > 0) ? max : NaN;
//…
首先,您需要定义"date"或使用"new Date()"。其次,还有其他参数需要设置,包括回调和要更新的目标元素。我建议阅读文档 here。
这是自述文件中的示例:
var timerId =
countdown(
new Date(),
function(ts) {
document.getElementById('pageTimer').innerHTML = ts.toHTML("strong");
},
countdown.HOURS|countdown.MINUTES|countdown.SECONDS);
// later on this timer may be stopped
window.clearInterval(timerId);
最后,您实际上并没有在我能看到的任何地方调用 getAge 函数。你只是定义了它。如果你 运行:
getAge(new Date());
您不会收到任何错误,但它不会执行任何操作,因为您尚未定义任何其他参数。