Solved

Set Timeout Giving Only last value

Posted on 2011-09-30
5
242 Views
Last Modified: 2012-05-12
<html>
<head>
<script language="javascript">

function starttime(limit)
{
limit=parseInt(limit);
document.getElementById("yes").innerHTML=limit;
if(limit>0)
{
limit--;
setTimeout(starttime(limit),1000);
}
else
{
document.getElementById("yes").innerHTML="Advertisement Visited Succesfully";
}
}
</script>
</head>
<body onload="starttime(<?php echo $_REQUEST[time]?>)">
<div id="yes">
</div>
</body>
</html>
0
Comment
Question by:savsoft
  • 3
5 Comments
 

Author Comment

by:savsoft
ID: 36890502
When I run this code It will display only last value of limit.......i.e when condition get false but i want ke it will run like  suppose limit is 30.. it is 30 29 28 27 26 25 24 23 22 21 20...................................4 3 2 1 0
0
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
ID: 36890567
make the limit variable global to this function starttime(), and set the value of limit variable in onload

<html>
<head>
<script language="javascript">
var limit;
function starttime()
{
limit=parseInt(limit);
document.getElementById("yes").innerHTML=limit;
if(limit>0)
{
limit--;
setTimeout(starttime(limit),1000);
}
else
{
document.getElementById("yes").innerHTML="Advertisement Visited Succesfully";
}
}
</script>
</head>
<body onload="limit=<?php echo $_REQUEST[time]?>;starttime()">
<div id="yes">
</div>
</body>
</html>
0
 

Assisted Solution

by:savsoft
savsoft earned 0 total points
ID: 36890581
SIR MORE RELIABLE ANSWER....................................

Working Fine is

<html>
<head>
<script language="javascript">

function starttime(limit)
{
limit=parseInt(limit);
document.getElementById("yes").innerHTML=limit;
if(limit>0)
{
limit--;
setTimeout("starttime("+limit+")",1000);
}
else
{
document.getElementById("yes").innerHTML="Advertisement Visited Succesfully";
}
}
</script>
</head>
<body onload="starttime(<?php echo $_REQUEST[time]?>)">
<div id="yes">
</div>
</body>
</html>
0
 

Author Closing Comment

by:savsoft
ID: 36915427
Both answers are correct
0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36890604
Check this


<html>
<head>
<title>Kiran Test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function()
{
  starttime(10);
});

function starttime(limit)
{
  limit=parseInt(limit);
  document.getElementById("yes").innerHTML=limit;
  if(limit>0)
  {
    limit--;
    $("#yes").html(limit);
    setTimeout("starttime("+limit+")",1000);
  }
  else
  {
    $("#yes").html("Advertisement Visited Succesfully");
  }
}

</script>
</head>
<body>
  <div id="yes">kiran
</div>
</body>
</html>

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question