Javascript syntax highlighter infinite loop

137 views Asked by At

I'm making a syntax highlighter in Javascript and HTML. It works fine at the moment but I think it's really inefficient because I have an interval with a time of 0 which runs a function that loops through all of the characters in the text area and then inserts them into a div behind the text area to provide the syntax highlighting.

I think my lexer is really bad too, but at the moment I'm more concerned with the function running like a million times a second that loops through every character in the text area each time.

Can anyone please think of a more efficient way to do this?

There doesn't seem to be any performance problems but I'm not sure if it will work on a lower-powered machine because I don't want it to crash the browser tab because I want to have several on a page so I need it to be as efficient as possible.

I understand that its annoying to be given loads of code and asked to help, but I thought for the problem to be easiest to debug you'd need the entire code.

Here you code:

<html>
<head>
    <title>My Syntax Highlighter</title>
    <style type="text/css">
    #container {
        width: 100%;
        height: 100%;
        position: relative;
        padding: 0px;
    }

    #code {
        width: 100%; 
        height: 100%; 
        outline:none;
        position: absolute;
        background-color: transparent;
        border: none;
        font-family: Courier;
        font-size: 22px;
        color:  rgba(0,0,0,.2) !important;
     }

     #codeb {
        width: 100%;
        height: 100%;
        position: absolute;
        font-family: Courier;
        font-size: 22px;
        padding: 2px 2px;
        color: #000;
     }

     .keyword {
        /*font-weight: bold;*/
        color: #E42D82;
     }

     .string {
        /*font-weight: bold;*/
        color: #0086b3;
     }

    </style>
    <script type="text/javascript">


    function u() {
        var code = document.getElementById("code");
        var codeb = document.getElementById("codeb");
        var c = "";
        var tok = "";
        var cc = 0;
        var t = "";
        var takeaway = 0;

        var stringStarted = false;
        var string = "";

        for (var i = 0; i < code.value.length; i++) {

            tok += code.value[i];
            c += code.value[i];

            cc++;

            if (tok == "print") {
                t = "<span class=\"keyword\">print</span>";
                takeaway += 5;
                c = c.substring(0, cc - takeaway) + t + c.substring(cc + t.length);
                cc += t.length;
                tok = "";
            } else if (tok == "var") {
                t = "<span class=\"keyword\">var</span>";
                takeaway += 3;
                c = c.substring(0, cc-takeaway) + t + c.substring(cc + t.length);
                cc += t.length;
                tok = "";
            } else if (tok == "\"") {
                tok = "";
                if (stringStarted == false) {
                    stringStarted = true;
                    string += "\"";
                } else {
                    stringStarted = false;
                    string += "\"";
                    t = "<span class=\"string\">" + string + "</span>";

                    takeaway += string.length;
                    c = c.substring(0, cc-takeaway) + t + c.substring(cc + t.length);
                    cc += t.length;
                    tok = "";
                    string = "";

                }
                tok = "";
            } else if (tok == " ") {
                if (stringStarted == true) {
                    string += " ";
                }
                c+= "";
                tok = "";
            } else {
                if (stringStarted == true) {
                    string += code.value[i];
                    tok = "";
                }
            }


            codeb.innerHTML = c;
            //console.log("test");

        };

        //alert(string);

        if (code.value == "") {
            codeb.innerHTML = "";
        }

        clearI(setInterval(function(){ u(); }, 0));


    }

    function clearI(interval) {
        clearInterval(interval);
    }

    var interval = setInterval(function(){ u(); }, 0);  

    </script>
</head>
<body>
<div id="container">
    <div id="codeb"></div>
    <textarea id="code" autofocus></textarea>
</div>
</body>
</html>
0

There are 0 answers