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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style type="text/css">
        #main{border:1px solid black; width:200px;margin:0px auto;padding:100px;}
    </style>
    <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) {
                seconds--;
                document.getElementById("msg").innerHTML = seconds + " Auto off in seconds! ";
            }
            else {
                window.close();
            }
        }
    </script>
</head>
<body>
    <div id="main">
         This is the advertising form :
        <div id="msg">5 Auto off in seconds! </div>
    </div>
</body>
</html>


Related articles: