doujianqin5172 2011-04-13 10:42
浏览 38
已采纳

2个javascript函数冲突?

I am trying to implement a Javascript/PHP/AJAX clock into my website so that I can have a simple clock which can operate in different timezones (tutorial is here http://networking.mydesigntool.com/viewtopic.php?tid=373&id=31)

This itself works fine, but I already have a javascript stopwatch running on the page, and the 2 seem to clash and the clock won't display while the stopwatch is working.

This is the script for the clock:

<script type="text/javascript">
function loadTime ()
{
http_request = false;
if(window.XMLHttpRequest)
{
    // Mozilla, Safari,...
    http_request = new XMLHttpRequest();
    if(http_request.overrideMimeType)
    {
        // set type accordingly to anticipated content type
        //http_request.overrideMimeType('text/xml');
        http_request.overrideMimeType('text/html');
    }
}
else if(window.ActiveXObject)
{ // IE
    try
    {
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
        try
        {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e)
        {
        }
    }
    }

    var parameters = "time=";
    http_request.onreadystatechange = alertContents;
    http_request.open('POST', 'time.php', true);
    http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http_request.setRequestHeader("Content-length", parameters.length);
    http_request.setRequestHeader("Connection", "close");
    http_request.send(parameters);
}


function alertContents()
{
    if (http_request.readyState == 4)
    {
        if (http_request.status == 200)
        {
            result = http_request.responseText;
            document.getElementById('clock').innerHTML = result;
        }
    }
}

</script>
<body onload="setInterval('loadTime()', 200);">

and this is the code for the stopwatch:

<script type="text/javascript">
    window.onload = function()
    {
        stopwatch('Start');
    }

    var sec = 0;
    var min = 0;
    var hour = 0;
    function stopwatch(text) {
        sec++;
        if (sec == 60) {
            sec = 0;
            min = min + 1;
        } else {
            min = min;
        }
        if (min == 60) {
            min = 0; 
            hour += 1;
        }

        if (sec<=9) { sec = "0" + sec; }
        document.clock.stwa.value = ((hour<=9) ? "0"+hour : hour) + " : " + ((min<=9) ? "0" + min : min) + " : " + sec;

        if (text == "Start") { document.clock.theButton.value = "Stop "; }
        if (text == "Stop ") { document.clock.theButton.value = "Start"; }

        if (document.clock.theButton.value == "Start") {
            window.clearTimeout(SD);
            return true;
        }
        SD=window.setTimeout("stopwatch();", 1000);
    }

    function resetIt() {
        sec = -1;
        min = 0;
        hour = 0;
        if (document.clock.theButton.value == "Stop ") {
            document.clock.theButton.value = "Start";
        }
        window.clearTimeout(SD);
    }
</script>

Could someone help me get them to work side-by-side please?

Thanks for any help

  • 写回答

2条回答 默认 最新

  • doufei3152 2011-04-13 11:11
    关注

    For one, your’re declaring an onload event handler in your HTML:

    <body onload="setInterval('loadTime()', 200);">
    

    which is consequently overwritten in script:

    window.onload = function()
    {
        stopwatch('Start');
    }
    

    This means the original onload call is never executed.

    You should try using addEventListener so you can add multiple event handlers to the same event.

    A couple more points:

    • Don’t pass a string to setInterval and setTimeout, just pass the function itself. More efficient and less error-prone: setInterval(loadTime, 200);
    • Instead of writing all that JS code to work with different browsers, use jQuery, mootools, or one of the gazillion other frameworks. They make it a lot easier to get it right on all browsers.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退