Solved

Set Timeout Giving Only last value

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

763 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