English 中文(简体)
希望在二次到间隔时间之后一例显示
原标题:want to show number one by one after a second through interval
  • 时间:2012-01-12 10:39:47
  •  标签:
  • javascript

i 想在二次后一度显示数字,但低于编码,显示总数。

function showVal(){
    for(i=0; i<5; i++)
    {
        var showingVal= document.write("the number is "+i);
        document.write("<br />");       
    }
}
setInterval( showVal() ,1000);
最佳回答

更能使用<条码>准时<>。

function showVal(n) {
  if(!n)
    n = 0;
  var showingVal = document.write("the number is " + n);
  document.write("<br />");       

  if(n < 4) {
    setTimeout(function() { showVal(n + 1); }, 1000);
  }
}

setTimeout(showVal, 1000);

页: 1

这比<条码>版Interval好,因为在你印刷了5个数字之后,你打上了记号。 效率。 + 无全球变量:-

或者,你可以读取“密码>。

问题回答

不要在功能内使用 lo,而是使用一种全球变量,在功能内加以使用,如果该数目达到极限,则进行检查。

您可在间隔期内使用<代码>文件>write,因为这在文件完成后将写到文件上,然后由案文取代文件。

此外,该职能中的守则有时只产出一个编号,它一度产出所有数字。 你们需要超越职能范围,以便随时了解职能在哪里。

使用<代码>clear Interval,一旦其输出所有数字,即停止使用。

在该网页上已经存在的要素中添加案文。 将该守则放在该页的后面,以便在该法典开始时建立该要素。

<div id="SomeElement"></div>

<script type="text/javascript">

var handle, counter = 0, text =   ;

function showVal(){
  if (counter < 5) {
    text += "the number is " + counter + "<br />";       
    document.getElementById("SomeElement").innerHTML = text;
    counter++;
  } else {
    window.clearInterval(handle);
  }
}

handle = window.setInterval(showVal, 1000);

</script>

Demo: http://jsfiddle.net/Guffa/CY5DV/

(可以在关闭时这样做,以便它能够以变数污染全球范围,但对于该问题的主要问题来说,这并不重要)。)

function printCount() {
    for (let i = 1; i <= 5; i++) {
       setTimeout(function() {
            document.write(i + "<br>");
            }, i * 1000);
        }
   }
   printCount();




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签