Results 1 to 6 of 6

Thread: how to stop a javascript function from running? i include my example here.

  1. #1
    Join Date
    Aug 2008
    Location
    karanganyar, solo, indonesia
    Posts
    161
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default how to stop a javascript function from running? i include my example here.

    hello...

    i have a function that running on my own site, i can create a function , but i'm not able to stop it,
    please my example below...

    <html>
    <head>
    <title>percobaan
    </title>
    <script type="text/javascript">

    function alertku(opt){
    if(opt=="run"){
    var t = document.getElementById('alertku').innerHTML+="run ,";

    setTimeout("alertku('run')",1000);
    }
    else if(opt=="stop") {return false;}
    }
    </script>
    </head>
    <body>
    <input type="button" value="RUN" onclick="alertku('run');"> <input type="button" value="STOP" onclick="alertku('stop');"><br>

    <span id="alertku">hum </span>
    </body>
    </html>
    please, when i click stop, the function is still running....
    can anyone help?
    ///////////////////////////////////////////////////
    ///// http://www.mediatutorial.web.id
    ///////////////////////////////////////////////////

  2. #2
    Join Date
    Aug 2008
    Location
    karanganyar, solo, indonesia
    Posts
    161
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    hi, i have done!

    use clear timeout to stop this...!

    function alertku(opt){
    if(opt=="run"){
    var t = document.getElementById('alertku').innerHTML+="run ,";
    exe = setTimeout("alertku('run')",1000);
    }
    else if(opt=="stop") {
    exe = clearTimeout(exe);

    }
    }
    ///////////////////////////////////////////////////
    ///// http://www.mediatutorial.web.id
    ///////////////////////////////////////////////////

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,076
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    A better way:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Alertku - Demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    function alertku(){
     if(alertku.opt){
      document.getElementById('alertku_span').firstChild.nodeValue += ', run';
      setTimeout(alertku, 1000);
     }
    };
    </script>
    </head>
    <body>
    <div>
    <input type="button" value="RUN" onclick="alertku.opt = true; alertku();"> 
    <input type="button" value="STOP" onclick="alertku.opt = false;"><br>
    <span id="alertku_span">hum</span>
    </div>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    A better way:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
      <head>
        <title>Page Without Title</title>
        <script type="text/javascript">
          var Alertku = (function() {
            var tid;
    
            function start() {
              if (!tid)
                tid = setInterval((function(f) { f(); return f; })(tick), 1000);
            }
    
            function tick() {
              var el = document.getElementById('alertku_span');
    
              (el.firstChild || el.appendChild(document.createTextNode(""))).nodeValue += ', run';
            }
    
            function stop() {
              if (tid)
                clearInterval(tid);
    
              tid = null;
            }
    
            return { start: start,
                     stop:  stop };
          })();
        </script>
      </head>
      <body>
        <div>
          <input type="button" value="RUN" onclick="Alertku.start();"> 
          <input type="button" value="STOP" onclick="Alertku.stop();">
          <span id="alertku_span">hum</span>
        </div>
      </body>
    </html>
    John, beware that presentational <br>.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,076
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey View Post
    John, beware that presentational <br>.
    Valid strict as used in my code, harms nothing that I can see, please elaborate. Unless you mean just in general. If that's what you are getting at, with such short lines (as in this case), I consider it acceptable.

    Breaking long lines with a <br> is stupid though, because one never knows how they will otherwise layout in the viewport.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Valid, yes, but used presentationally. It's on the same order as tables for layout. Properly, it should be written using CSS display: block; or so.

    Here's an abstraction of this behaviour:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
      <head>
        <title>Page Without Title</title>
        <script type="text/javascript">
          function Service(f, opts) {
            this.tid = null;
            this.f = f;
            this.interval = opts.interval || 1000;
            this.init = opts.init || Service.NONE_FUNCTION;
            this.final = opts.final || Service.NONE_FUNCTION;
            this.stopCond = opts.stopCond || Service.NONE_FUNCTION;
          }
    
          Service.prototype = {
            start: function() {
              if (!this.tid) {
                this.init();
                this.tick();
                this.tid = setInterval(function() { this.tick(); }, this.interval);
              }
            },
    
            tick: function() {
              if (this.stopCond())
                this.stop();
              else
                this.f();
            },
    
            stop: function() {
              if (this.tid && !this.final) {
                clearInterval(this.tid);
                this.tid = null;
              }
            }
          };
    
          Service.NONE_FUNCTION = function() { return false; };
    
          function alertku() {
            var el = document.getElementById('alertku_span');
    
            (el.firstChild || el.appendChild(document.createTextNode(""))).nodeValue += ', run';
          }
    
          var sAlertku = new Service(alertku);
        </script>
      </head>
      <body>
        <div>
          <input type="button" value="RUN" onclick="sAlertku.start();"> 
          <input type="button" value="STOP" onclick="sAlertku.stop();">
          <span id="alertku_span">hum</span>
        </div>
      </body>
    </html>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •