簡體   English   中英

if(true),則document.write但不起作用

[英]if(true), then document.write but doesn't work

    <script>

    var now = new Date();
    document.write(now.getMinutes() + " " + now.getSeconds() + " "
            + now.getMilliseconds());

    if (now.getSeconds() == 0) {
        var time = now.getMilliSeconds();
        document.write("HELLO");
        document.write("<form action=\"Second\" method=\"GET\">");
        document.write("<input type=\"hidden\" value=\"temp\" id=\"a\" />");
        document.getElementById("a").value = time;
        document.write("<input type=\"submit\" value=\"submit\" /> </form>");
    }
    else {
        window.location.reload("http://localhost:8080/firstweb/NewFile1.jsp");
    }
</script>

我正在嘗試編寫一個js代碼,以檢查通過刷新達到指定的秒數的毫秒數。 但是表面上,document.write()並沒有做任何事情。 只要達到0秒,它就會

document.write(now.getMinutes() + " " + now.getSeconds() + " "
        + now.getMilliseconds());

這不是if語句中的內容。 我該如何解決?

ps是否有可能(例如在9:00鍾時)使某些代碼運行?

您的腳本在頭。 該文檔在加載(和運行)時可能不存在。 另外,您確實不需要重新加載同一文檔即可重新執行腳本。 這將導致大量的請求。 嘗試將腳本放入函數中,然后從window.setTimeout()進行調用。

<script>

var now = new Date();
document.write(now.getMinutes() + " " + now.getSeconds() + " "
        + now.getMilliseconds());

if (now.getSeconds() == 0) {
   var time = now.getMilliseconds();
    document.write("HELLO");
    document.write("<form action=\"Second\" method=\"GET\">");
    document.write("<input type=\"hidden\" value=\"temp\" id=\"a\" />");
    document.getElementById("a").value = time;
    document.write("<input type=\"submit\" value=\"submit\" /> </form>");

}
else {
    window.location.reload("http://localhost:8080/firstweb/NewFile1.jsp");
}

試試它對我有用 在此處輸入圖片說明

令人難以置信的是,這行代碼無法正常工作:

    var time = now.getMilliSeconds();

秒的秒數應更低,因此:

    var time = now.getMilliseconds();

它將正常工作!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM