开发者

2 javascript functions clashing?

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 (开发者_如何学Ctutorial 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


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.


Try this: See the subtle '+=' instead of '=' !

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜