Requirement
Implementation
<script src="http://code.jquery.com/jquery-1.7.2.min.js" language="javascript"></script>
<script language="javascript"> $(document).ready(function () { var blink = null; $("#btntoggle").on("click", function () { if (blink == null) blink = setInterval(blinkMessage, 500); }); $("#btnstoptoggle").on("click", function () { if (blink != null) { clearInterval(blink); blink = null; $(".divmsg").removeClass("divred"); } }); }); function blinkMessage() { $(".divmsg").toggleClass("divred"); } </script>
HTML Markup
<!DOCTYPE html> <html> <head> <title>JQuery Blinking Example</title> <script src="http://code.jquery.com/jquery-1.7.2.min.js" language="javascript"></script> <meta charset="utf-8" /> <style type="text/css"> .divmsg { background: #ff0097; color: #fff; display: inline-block; margin: 20px 10px 20px 10px; padding: 20px; border-radius: 5px; width: 30%; font-family: Verdana; font-weight: bold; } .divred { background: #223c88 !important; } .btn { border: 2px solid black; background-color: white; color: black; padding: 14px 28px; font-size: 16px; cursor: pointer; margin: 0 0 0 10px; } /* Green */ .success { border-color: #ff0097; color: #223c88; } .success:hover { background-color: #ff0097; color: white; } /* Blue */ .info { border-color: #223c88; color: #ff0097; } .info:hover { background: #223c88; color: white; } </style> </head> <body> <div class="divmsg"> Hi...! Codingvila, This is Nikunj Satasiya </div><br /> <input type="button" id="btntoggle" class="btn success" value="Start Blinking" /> <input type="button" id="btnstoptoggle" class="btn info" value="Stop Blinking" /> <script language="javascript"> $(document).ready(function () { var blink = null; $("#btntoggle").on("click", function () { if (blink == null) blink = setInterval(blinkMessage, 500); }); $("#btnstoptoggle").on("click", function () { if (blink != null) { clearInterval(blink); blink = null; $(".divmsg").removeClass("divred"); } }); }); function blinkMessage() { $(".divmsg").toggleClass("divred"); } </script> </body> </html>
Output/Demo
Summary
This article explains how you can blink html div tag using jquery toggleClass and also start and stop blinking using the javascript setInterval and clearInterval functions.