0

I have some issues starting this function what am i doing wrong?

I want this to start writing when my menu has reached the section "topb".

This is the attempt from me:

var $el = $('.writer'),
    txt = $el.text(),
    txtLen = txt.length,
    timeOut,
    $topb = $('#topb'),
    char = 0;

$el.text('|');

if (topb.hasClass('active')) {
    (function typeIt() {
        var humanize = Math.round(Math.random() * (200 - 30)) + 30;
        timeOut = setTimeout(function () {
            char++;
            var type = txt.substring(0, char);
            $el.text(type + '|');
            typeIt();

            if (char == txtLen) {
                $el.text($el.text().slice(0, -1)); // remove the '|'
                clearTimeout(timeOut);
            }
        }, humanize);
    }())
};

This is the original function

var $el = $('.writer'),
    txt = $el.text(),
    txtLen = txt.length,
    timeOut,
    char = 0;

$el.text('|');

(function typeIt() {
    var humanize = Math.round(Math.random() * (200 - 30)) + 30;
    timeOut = setTimeout(function () {
        char++;
        var type = txt.substring(0, char);
        $el.text(type + '|');
        typeIt();

        if (char == txtLen) {
            $el.text($el.text().slice(0, -1)); // remove the '|'
            clearTimeout(timeOut);
        }
    }, humanize);
}())
};
2
  • 1
    $topb = $('#topb') and topb.hasClass('active'), you forgot about "$" sign. Commented Sep 15, 2014 at 11:42
  • @pushOk: true (already mentioned below), but that is the least of their problems as that code is never run again :) Commented Sep 15, 2014 at 13:09

1 Answer 1

1

The original code uses a timer to repeat the operation until a certain condition is met (calls itself recursively). You have placed your if check outside of that, so it never runs.

Do your check inside the timer, so it is checked repeatedly. Like this:

var $el = $('.writer'),
    txt = $el.text(),
    txtLen = txt.length,
    timeOut,
    $topb = $('#topb'),
    char = 0;

$el.text('|');

(function typeIt() {
    if ($topb.hasClass('active')) {
        var humanize = Math.round(Math.random() * (200 - 30)) + 30;
        timeOut = setTimeout(function () {
            char++;
            var type = txt.substring(0, char);
            $el.text(type + '|');
            typeIt();

            if (char == txtLen) {
                $el.text($el.text().slice(0, -1)); // remove the '|'
                clearTimeout(timeOut);
            }
        }, humanize);
    }
    else{
        // do nothing but repeat the timer (e.g. after 2 seconds)
        timeOut = setTimeout(typeIt, 2000);
    }
}());
Sign up to request clarification or add additional context in comments.

4 Comments

Okey but it dosent work even if i dosent use the if statement to start it @TrueBlueAussie
There may be more than one bug... This was a guideline to fix the obvious one. A JSFiddle example is usually the best way to demonstrate a problem like this. Then we can all guarantee it works completely :)
Ok i will test some more and se if i can fix it :) thank you for your awnser! @TrueBlueAussie
Note: There was a typo in my example... now fixed (a $ was missing on $topb.hasClass('active'))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.