Solved

Set Timeout Giving Only last value

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
comparing strings that contain line breaks 18 35
Menu Inconsistent 3 20
regex expression 9 21
showing numeric numbers 2 8
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 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

17 Experts available now in Live!

Get 1:1 Help Now