Javascript setInterval's two call methods of the of instance

  • 2020-03-30 00:01:19
  • OfStack

As follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
    <style type="text/css">
        #main{border:1px solid black; width:200px;margin:0px auto;padding:100px;}
    <script type="text/javascript">
        window.onload = function () {
            //The first method is called, passing a method pointer
            //setInterval(showMsg, 1000);
            //The second method is called
            var methodName = "showMsg()";    //Notice the use of strings and parentheses
            setInterval(methodName, 1000);   //I put double quotes around it to indicate that there's code inside, and I'm going to execute the code inside like setInterval("alert('a')",1000), and I'm going to execute alert('a') every 1 second
        var seconds = 5;
        function showMsg() {
            if (seconds > 0) {
                document.getElementById("msg").innerHTML = seconds + " Auto off in seconds! ";
            else {
    <div id="main">
         This is the advertising form :
        <div id="msg">5 Auto off in seconds! </div>

Related articles: