简体   繁体   English

如何让Java语言进行循环显示以实时计数进行循环的次数?

[英]How can I take a Javascript for loop display the number of times it has looped with a live count?

How can I take a Javascript for loop display the number of times it has looped with a live count? 如何让Java语言进行循环显示以实时计数进行循环的次数?

for (var i = 0; i < 10; i ++) {
    [ DO FUNCTION HERE ]
    document.getElementsByTagName("span")[3].innerHTML = "Cycles: " + i;
}

I want it to show "Cycles: #" and have the # increment live as it is looping through each function. 我希望它显示“ Cycles:#”(循环:#),并在每个函数中循环显示#增量。 Similar to a loading screen. 类似于加载屏幕。 Ideas? 有想法吗?

This will update your display once per second. 这将每秒更新一次显示。

DEMO: http://jsfiddle.net/Lb3Uc/ 演示: http : //jsfiddle.net/Lb3Uc/

var span = document.getElementsByTagName("span")[3];
var i = 0;
(function looper() {
    if( i++ < 10 ) {
        span.innerHTML = "Cycles: " + i;
        setTimeout( looper, 1000);
    }
})();

Every time the looper function runs, as long as i < 10 it recursively invokes itself after a 1000ms delay. 每当looper函数运行时,只要i < 10它就会在1000ms延迟后递归调用自身。

This sort of asynchronous code is required in order to allow the page to redraw. 为了允许页面重绘,需要这种异步代码。

How about something like this: 这样的事情怎么样:

var out = document.getElementsByTagName("span")[3];
var speed = 1;
var i = 0;
function loop() {
    out.innerHTML = "Cycles " + i;
    // do something
    if (++i < 10) {
        setTimeout(loop, speed);
    }
}
loop();

If it's happening too fast to watch the time, just increase speed . 如果发生时间太快而无法观看时间,请提高speed If you set speed to 100, you'll see the counter increment about 10 times per second, depending on how long it takes for your code to execute. 如果将speed设置为100,您将看到计数器每秒增加约10次,具体取决于代码执行所需的时间。

Your for loop is actually doing exactly what you'd like it to do, but the javascript is executing so quickly you're getting straight to the final cycle faster than you can notice it changing. 实际上,您的for循环确实在执行您想要的操作,但是javascript的执行速度如此之快,以至于您进入最后一个循环的速度比您所注意到的变化要快。 Try writing your var i to the console and you'll see what I mean. 尝试将您的var i写入控制台,您将明白我的意思。 It's printing on every cycle, but it seems almost instant because the script isn't taking any time to execute. 它在每个周期都在打印,但似乎几乎是即时的,因为脚本不需要花费任何时间来执行。

EDIT: 编辑:

Per your request, here's how you would slow your application down so you could implement your timer... 根据您的请求,这是降低应用程序速度以便实现计时器的方法...

var i = 0;
var interval = setInterval(
    function() { 
        /* add the rest of your method here */
        document.getElementsByTagName("span")[3].innerHTML = "Cycles: " + i;
        i++; 
        if(i > 10) { /* number of repetitions */
            clearInterval(interval);
        }
    },
1000); /* seconds delayed */

In your function you are not adding but replacing the content of your span. 在您的函数中,您不是在添加而是替换跨度的内容。 Use the += operator instead 改用+=运算符

http://jsfiddle.net/9wKQw/ http://jsfiddle.net/9wKQw/

for (var i = 0; i < 10; i ++) {
    document.getElementById("oput").innerHTML += "Cycles: " + i + "<br>";
}

To coordinate the AJAX function and the progress update view, don't artificially slow down the function calls. 为了协调AJAX函数和进度更新视图,请不要人为地减慢函数调用的速度。 For one thing, it will reduce response time, which the user won't like. 一方面,它将减少用户不希望的响应时间。 For another, the progess will be off by one. 另一方面,进展将一口气。

Instead, have the AJAX function call a function that updates the progress when the AJAX call completes either successfully or conditionally, depending on what's appropriate. 取而代之的是,让AJAX函数调用一个函数,该函数会在AJAX调用成功或有条件地完成时更新进度,具体取决于适当的条件。

Lastly, you may need to change the document structure, which will necessitate changing how you get the progress element. 最后,您可能需要更改文档结构,这将有必要更改获取进度元素的方式。 Instead, give the progress element an ID and use getElementById . 相反,给progress元素一个ID并使用getElementById

var async_done = (function() {
    var doneCount = 0;
    return function () {
        ++doneCount;
        document.getElementById("Progress").innerHTML = "Cycles: " + doneCount;
    };
})();

for (var i = 0; i < 10; i ++) {
    async_function(async_done);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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