0

Ok, so here is what I've gathered together so far. I've got a countdown timer, that at the end of 180 seconds it executes the function clearit. The problem I'm having I would like to put a user chosen variable in place of the 180. Any help would be greatly appreciated.
Thank you.

<script type="text/javascript">
function starttimer8() {
/* set your parameters(
number to countdown from,
pause between counts in milliseconds,
function to execute when finished
)
*/
startCountDown(180, 1000, clearit);
}

function startCountDown(i, p, f) {
// store parameters
var pause = p;
var fn = f;
// make reference to div
var countDownObj = document.getElementById("countDown");
if (countDownObj == null) {
// error
alert("div not found, check your id");
// bail
return;
}
countDownObj.count = function(i) {
// write out count
this.innerHTML = i;
if (i == 0) {
// execute function
fn();
// stop
return;
}
setTimeout(function() {
// repeat
countDownObj.count(i - 1);
},
pause
);
}
// set it going
countDownObj.count(i);
}
</script>
<div id="countDown"></div>
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Taywin
0

Can't you just change it to...

function starttimer8(t) {
/* set your parameters(
number to countdown from,
pause between counts in milliseconds,
function to execute when finished
)
*/
startCountDown(t, 1000, clearit);
}

If I were you, I would code it differently... Sorry, but your code is not my style. :) Anyway, my code would look like below (just my 2 cents).

// JavaScript part  (count.js)
/*****
 * class constructor
 *
 * @param  vName  a string of variable name
 * @param  displayId  a string of div element ID
 * @param  func  a function pointer
 */
function CountingKlass(vName, displayId, func) {
  if (vName && vName.replace(/\s+/)!="") {
    this.varName = vName
    if (document.getElementById(displayId)) {
      this.displayArea = document.getElementById(displayId)
    }
    else { alert("Not found display div ID - "+displayId.toString()) }
    if (func) {
      this.funcCall = func
    }
    else { alert("Not found assigned function") }
  }
}  // new CountingKlass(string, string, function)


/*****
 * Count down the loop using user-defined set up
 *
 * @param  timeLength  an integer of counting down time
 * @param  pause  an integer of waiting time length for each pause
 */
CountingKlass.prototype.countdown = function(timeLength, pause) {
  this.displayArea.innerHTML = timeLength
  timeLength = parseInt(timeLength, 10) - 1
  if(timeLength<0) {  // done
    this.funcCall()
  }
  else {
    setTimeout(this.varName+".countdown("+timeLength+","+pause+")", parseInt(pause,10))
  }
}  // countdown(int, int)


/*****
 * Add more info of this class object to toString()
 */
CountingKlass.prototype.toString = function() {
  var out = "Variable Name: "+this.varName+"\n"
  out += "Display: "+((this.displayArea)? this.displayArea.id : "None")+"\n"
  out += "Function: "+((this.funcCall)? this.funcCall : "None")+"\n"

  return out
}

//===============================================
// HTML
<html>
<head>
  <script type="text/javascript" src="count.js"></script>
  <script type="text/javascript">
  function callAlert() {
    alert("Done")
  }
  </script>
</head>

<body>
  <div id="disp_area">10</div>
  <script type="text/javascript">
  var cnt = new CountingKlass("cnt", "disp_area", callAlert)
  cnt.countdown(5, 1000)
  </script>
</body>

</html>

One thing about using external function call is that it may be dangerous because the external function could break your whole program or cause unexpected behaviors. Also, it is trivia to call a function without argument, but it would become a little more complicated if you want to call a function with argument -- need to make the function a closure.

Edited by Taywin: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.