0

I have this function which loops through a list of strings every 5 seconds. I would like it to stay on the last string after finishing the loop. What do I need to change here?

window.specialWorkBoxStyleOverride = function(workBox) {
    var statusTextBox = $("<div class = 'status-description-box'></div>");
    $(workBox).append(statusTextBox);
    var statusTexts = ["Checking", "Updating", "Processing", "Saving"];
    var idx = 0;
    var updateStatus = function() {
        statusTextBox.text(statusTexts[idx]);
        idx = (idx + 1) % statusTexts.length;
        setTimeout(updateStatus, 5000);
    };
    updateStatus();
};

Thanks a lot.

0

1 Answer 1

2

Right now your code always calls setTimeout, so it will loop forever. Checking whether you've reached the end of your list should be sufficient to stop this (and means you don't need the modular arithmetic any more):

var updateStatus = function() {
    statusTextBox.text(statusTexts[idx]);
    idx++;
    if (idx < statusTexts.length) { 
        setTimeout(updateStatus, 5000);
    }
};
Sign up to request clarification or add additional context in comments.

1 Comment

Very nice. Thanks so much, Alex.

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.