[英]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>
它抛出一个错误
倒计时未定义
我将文件下载到本地,当我尝试直接调用它时,它可以工作,但在函数内部时却不起作用。
这是因为 GitHub 提供的原始文件没有设置正确的 MIME 类型,因此浏览器出于安全原因不会下载它们。
取而代之的是,使用一些替代的慷慨主机,它们确实使用正确的 MIME 类型为其提供服务。
我通常使用的是githack.com
。 要使用它,只是改变githubusercontent
在URL中githack
得到:
https://raw.githubusercontent.com/mckamey/countdownjs/master/countdown.js
我没有得到“倒计时未定义”,我得到“日期未定义”。 现在我有机会查看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;
//…
首先,您需要定义“日期”或使用“new Date()”。 其次,还有其他参数需要设置,包括回调和要更新的目标元素。 我建议在这里阅读文档。
以下是自述文件中的示例:
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());
您不会收到任何错误,但它什么也不做,因为您还没有定义任何其他参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.