简体   繁体   中英

Javascript show variable every 50 ms

I want to make a little 'loading...' widget for my website, using javascript.

var percent=0;  
var message="Loading... "  
var per="%"  
function count(){  
    percent=percent+1;  
    if(percent==100){  
        alert("Loading end.")  
    }else{  
        setTimeout("count",50)  
        document.write(message)  
        document.write(percent)  
        document.write(per)  
    }

But it isn't running. I think I've got some mistake (or maybe totally wrong). How can I do this? I want to update the shown message every 50ms.

try

setInterval(count,50);

instead of setTimeOut("count",50)

You want to set an interval which runs every x milliseconds, passing in an anonymous function to call the function to call

var percent=0;  
var message="Loading... "  
var per="%"  
function count(){  
    percent=percent+1;  
    if(percent==100){  
        alert("Loading end.")  
    }else{  
        setInterval(function() { count() },50)  
        document.write(message)  
        document.write(percent)  
        document.write(per)  
    }
} <--- you were also missing this ending brace

Script:

var percent = 0;
var message = "Loading... ";
var per = "%";

$(document).ready(function () {
    count();
});

function count() {
    percent = percent + 1;
    if (percent == 100) {
        alert("Loading end.");
    } else {
        setTimeout(function () {
            count();
        }, 50);
        document.write(message);
        document.write(percent);
        document.write(per);
    }
}

see this fiddle for more http://jsfiddle.net/8nhmU/19/

See this jsfiddle

HTML:

<span id="loading"></span>

Javascript:

var percent = 0;
var message = "Loading...";
var per = "%";
var element = document.getElementById('loading');

var interval = setInterval(function() {
    element.innerHTML = message + percent + per;
    percent += 1;
    if(percent > 100) {
        clearInterval(interval)
    };
}, 50)

The code in your example is missing a great deal of semi-colons and the ending curly-bracket, but that's not the end-issue.

The "problem" with your call to setTimeout is that the first argument must be an actual function, not a string. If you remove the quotes around the call, it will work.

Here is a copy of your code, re-formatted:

var percent=0;  
var message="Loading... ";
var per="%";

function count() {
    percent++;

    if (percent == 100) {
        alert("Loading end.");
    } else {
        setTimeout(count, 50);
        document.write(message);
        document.write(percent);
        document.write(per);
    }
}

You are doing it wrong way. You should call the setInterval method when window loads. And when loading is completed, you should stop interval by clearing it using its ID.

var countId;
window.onload = function(){
    countId=setInterval(count,50);
}

function count(){
    if(per=99){
      clearInterval(countId);
    }
    per++;
    //show your message
}

try with interval and clear it when progress is finished:

<!DOCTYPE html>
<html>
    <head>
        <title>testing</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <div id="progress">MSG</div>
        <script type="text/javascript">
            var percent = 0;
            var message = "Loading... ";
            var per = "%";
            var dom = document.getElementById('progress');
            var iv = setInterval(function(){
                console.log(message);
                dom.innerHTML = ((percent++) + per +' '+ message);
                if(percent === 100){
                    console.log("Loading end.");
                    clearInterval(iv);
                    return false;
                }
            }, 50);
        </script>
    </body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM