toggle javascript function

I'm using this code to start a log file :

function startTail(str) {
if (str== "stop") {
stopTail();
} else {
t= setTimeout("getLog()",1000);
}
}

This is called using :

<button id="start" onclick="getLog('start');">

and stopped using :

<button id="stop" onclick="getLog('stop');">

Is there anyway I can change this to one button that will toggle start / stop ?

Answers:

Answer

Try:

<button id="start" value="Toggle Log" onclick="getLog('start', this);">

function startTail(str, element) {
    if (str == "stop") {
        stopTail();
        element.setAttribute('onclick', "getLog('start', this);");
    } else {
        element.setAttribute('onclick', "getLog('stop', this);");
    }
}   
Answer

You could try this

var flag = false;
function startTail() {
 if (flag)
  {
    stopTail();
    document.getElementById('start').value = "Start";
    flag = false;
  }
 else 
  {
    t= setTimeout("getLog()",1000);
    flag = true;
    document.getElementById('start').value = "Stop";
  }
}

<button id="start" onclick="startTail();" value="Start">

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.