0

I want to loop over an array continuously on click. Extra props if you can work in a delay between class switches :-)

I got this far:

// Define word
var text = "textthing";

// Define canvas
var canvas = 'section';

// Split word into parts
text.split();

// Loop over text
$(canvas).click(function() {

    $.each(text, function(key, val) {
        $(canvas).removeAttr('class').addClass(val);
    });

});

Which is not too far at all :-)

Any tips?

4
  • 1
    Er.. what again? Your question still doesn't say why you want to do this.. Commented Aug 23, 2012 at 5:37
  • This logic is flawed: var text = 'lorem'; text.split(); console.log(text) // lorem. Do this instead: var text = 'lorem'.split(''); console.log(text) // [l, o, r, e, m] Commented Aug 23, 2012 at 6:09
  • @elclanrs. Yes, that's more succinct but I wish to populate that text var via a input element later down the line, and this makes it clearer. Commented Aug 24, 2012 at 3:35
  • What I mean is that text.split() won't update the variable unless you assign it again. ie. text = text.split() Commented Aug 24, 2012 at 3:55

3 Answers 3

1

The following will wait until you click the selected element(s) in the var el. In this example var el = $('section') will select all <section>...</section> elements in your document.

Then it will start cycling through the values in cssClassNames, using each, in turn as the css class name on the selected element(s). A delay of delayInMillis will be used between each class change.

var cssClassNames = ['c1', 'c2', 'c3'];
var el = $('section');
var delayInMillis = 1000;

// Loop over text
el.click(function() {
  var i = 0;
  function f() {
    if( i >= cssClassNames.length ) {
      i = 0;
    }
    var currentClass = cssClassNames[i];
    i += 1;

    el.removeClass().addClass(currentClass);
    setTimeout(f, delayInMillis);
  }
  f();
});
Sign up to request clarification or add additional context in comments.

Comments

0

I believe you want a delay of X milliseconds between removing a class and adding a class. I'm not sure that you have to have the lines marked // ? or even that they do the job, but what you do have to have is a way to get the value's into the function. Also, the setTimeout anon function might not actually need the parameters, but it should give you an idea.

$(canvas).click(function() {

    $.each(text, function(key, val) {
        $(canvas).removeAttr('class')
        var $canvas = $(canvas) //?
        var class_val = val  //?
        setTimeout(function ($canvas, class_val) {
            $canvas.addClass(class_val);
        }, 2000);

   });

});

Edit: I'd do this instead function modify_element($element, class_name){ $element.removeClass('class'); setTimeout(function ($element) { $element.addClass(class_name); }, 1000); //adds the class 1 second after removing it }

$(canvas).click(function() {

    $.each(text, function(key, val) {
        setTimeout(modify_element($(canvas), val),2000);
        //this will loop over the elements with 2 seconds between elements
    });

});

Comments

0

"loop over an array continuously" this sounds like a infinite loop, I don't think you want that. About pausing the loop, this is possible, you can use this

Comments

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.