문제

Hi Web Development의 Masters, 먼저 나는 내 눈을 믿지 않았다고 말하고 싶습니다. IE7에서 잘 작동하는 JavaScript가 있고 Firefox에서는 그렇지 않습니다 !!! :)))) 작은 농담이었습니다. :) 그래서 나는 이미 당신에게 문제를 말했습니다 (농담이 아니 었습니다), 이제 JavaScript를 붙여 넣고 있습니다.

<script type="text/javascript">

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
var ms;
ms = %%CONTENT_REFRESH%% - 5;
var stop;
stop = 0;
var myvalue;

function display() {
    if (!stop) {
        setTimeout("display();", 1000);
    }
    thetime.value = myvalue;
}
function recalc() {
    var hours;
    var minutes;
    var seconds;

    ms = ms - 1;
    hours = Math.floor(ms / 3600);
    minutes = Math.floor(ms / 60);
    if (minutes < 10) {
        minutes = "0"+minutes;
    }
    seconds = ms - (minutes*60) - (hours*3600);
    if (seconds < 10) {
        seconds = "0"+seconds;
    }
    myvalue = hours+":"+minutes+":"+seconds;
    thetime.value = myvalue;
    if (myvalue == "0:00:00") {
        stop = 1;
    }
    if (!stop) {
        setTimeout("recalc();", 1000);
    }
}
// End -->
</SCRIPT>

이것은 내가 알고있는 아주 오래된 대본입니다. 내 Winamp와 사이트의 카운트 다운에서 현재 나머지 송 시간이 걸립니다. 그러나 내가 말했듯이, 그것은 Firefox에서는 작동하지 않습니다.

카운트 다운 타이머를 호출하는 본문 및 코드는 다음과 같습니다.

<body class="playlist_body" onLoad="recalc();display();">

Time Left In Song: <INPUT align="center" TYPE="text" Name="thetime" size=5 />

</body>

// 편집 : FireBug를보고 다음 오류를 보았습니다.

thetime is not defined
recalc()playlist.cgi (line 87)
function onload(event) { recalc(); display(); }(load )1 (line 2)
error source line: [Break on this error] thetime.value = myvalue;\n
도움이 되었습니까?

해결책

문제는 이름으로 DOM 요소에 액세스하고 있다는 것입니다.

다음 코드를 상단에 추가하여 변수를 선언합니다. thetime 요소, 추가 id="thetime" ~로 INPUT, 전화를 추가하십시오 init(); 안에 onload 에서 body 요소.

var thetime;

function init() {
    thetime = document.getElementById('thetime');
}

그건 그렇고, 당신은 텍스트 상자를 정기적으로 교체 할 수 있습니다. DIV 설정하여 요소 div의 신분증 thetime, 및 교체 thetime.value ~와 함께 thetime.innerHTML.

또한 문자열 대신 함수로 settimeout을 호출하는 것이 좋습니다. 당신은 교체해야합니다 "display();" 그리고 "recalc();" ~와 함께 display 그리고 recalc 각기.

다른 팁

즉, 이름 속성이있는 요소가 창 객체에 배치되는 "기능"이 있습니다.

<div name=foo></div>

가변 "foo"를 줄 것입니다-이것은 비표준입니다.

document.getElementByName("foo") 

타이머 출력 요소를 얻으려면.

var thetime = document.getElementById("thetime");

입력에 이름 = "Thetime"대신 id = "Thetime"을 추가하십시오.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top