js小白求助,代码如下,谢谢!

为什么我点击调用不了startClock()这个函数呢?虽然我也觉得我这样写很不妥~ ~,求解--<script>//显示时间window.onload=function (){var oBtn=document.getElementsByTagName("input"); oBtn[0].onclick=function showTime() { var oSpan=document.getElementById("spanTime"); var oDate=new Date(); oSpan.innerHTML=oDate.getFullYear()+"年"+(oDate.getMonth()+1)+"月"+oDate.getDate()+"日"+"&nbsp星期"+oDate.getDay()+"<br/>时间:"+oDate.getHours()+":"+oDate.getMinutes()+":"+oDate.getSeconds(); } //启动时钟 oBtn[1].onclick=function startClock() { i=window.setInterval(showTime(),1000); } //停止时钟 oBtn[2].onclick=function stopClock() { window.clearInterval(i); }}</script>现在不明白 i = window.setInterval(function() {showTime()}, 1000);和 i = window.setInterval(showTime, 1000);的区别额(-懿-;)?
2026年09月22日 11:35
有3个网友回答
网友(1):

你的启动时钟函数和停止函数写成一样的了,改掉其中一个的函数名就好了

网友(2):

var interval;
window.onload = function() {
    var oBtn = document.getElementsByTagName("input");
    var oSpan = document.getElementById("spanTime");

    function showTime() {
        var oDate = new Date();
        oSpan.innerHTML = oDate.getFullYear() + "年" + (oDate.getMonth() + 1) + "月" + oDate.getDate() + "日" + " 星期" + "日一二三四五六".split("")[oDate.getDay()] + "时间:" + oDate.getHours() + ":" + oDate.getMinutes() + ":" + oDate.getSeconds();
    }
    oBtn[0].onclick = function() {
        showTime();
    }
    oBtn[1].onclick = function() {
        if (!interval) {
            interval = setInterval(function() {
                showTime();
            }, 1000);
        }
    }
    oBtn[2].onclick = function() {
        if ( !! interval) {
            clearInterval(interval);
            interval = null;
        }
    }
}

网友(3):

那两种写法没有任何区别,你的错误在于

oBtn[0].onclick=function showTime() {

这句话中,showTime只是局部变量,oBtn[1]的click函数是无法访问的。你写成全局的就行了。

两种写法我也试过了,都可以正常启动、停止