Solved

Set Timeout Giving Only last value

Posted on 2011-09-30
5
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
&& operators not performing required logic 8 37
Error Handler 8 38
Php variable to be sent back 3 30
Ajax success not firing alert 6 36
When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

738 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