Solved

Set Timeout Giving Only last value

Posted on 2011-09-30
5
238 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

867 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now