Youss
Youss

Reputation: 4212

Create variables on the fly

Im creating code snippets dynamicly:

var gg = setInterval(function() {
    if($('p:visible').length > 0) {
        $('p').trigger('click') ;
        clearInterval(gg);
    }
},500);

The code works. The problem is that I create the same code snippets with different selectors:

var gg = setInterval(function() {
    if($('input:visible').length > 0) {
        $('input').trigger('focus') ;
        clearInterval(gg);
    }
},500);

So when I put(create) these codes together, the whole thing goes in to some loop and it doesn't work anymore, take a look at this example: JsFiddle

The problem is that I assign the same variable (gg) to the codes. So my question is: How do I create different variables every time I create these code snippets? I tried some things with var x=x++ but I can't get it to work.

Upvotes: 0

Views: 1960

Answers (4)

Jack
Jack

Reputation: 9388

Here are two ways of accomplishing what you want to do (dynamic variables). The first is evaling a string.

(There are a ton of articles out there that will explain why this is bad. But, for the sake of being complete, I'm providing this example...maybe as an example of what not to do ;)

A function that creates variables based on a string you pass in, and then using that eval'd variable would work. Something like:

function varMaker(str){
    // Nothing fancy...
    var token = Math.floor(new Date().getTime() * Math.random(10));

    // Defining variable with no var for global scope (bad)
    return eval(str + '_' + token + ' = undefined;');
}

Here's a quick/dirty fiddle example (no function maker, just an eval'd string): http://jsfiddle.net/D3wZ7/

The second, cleaner approach is using square bracket notation (and what I would recommend). Something like this:

window['gg' + randomized_string] = setInterval(..)

Note: Once you've defined the variable, you can reference it as you normally would as such:

window['my_cool_variable' + 1] = "stack";
alert(my_cool_variable1); //alerts "stack"

Here's a fiddle demonstrating this: http://jsfiddle.net/ZmGH5/

Good luck!

Upvotes: 2

Geylani ARCA
Geylani ARCA

Reputation: 308

var gg= [];
gg[0] = setInterval(function() {
    if($('p:visible').length > 0) {
        $('p').trigger('click') ;
        clearInterval(gg[0]);
    }
},500);

gg[1] = setInterval(function() {
    if($('input:visible').length > 0) {
        $('input').trigger('focus') ;
        clearInterval(gg[1]);
    }
},500);

so go on like this way. You can create lots of by using array.

Upvotes: 1

Martin
Martin

Reputation: 621

Simply put the snippets in anonym wrapper functions so that the variable gg is not global anymore:

(function(){
    var gg = setInterval(function() {
        if($('input:visible').length > 0) {
            $('input').trigger('focus') ;
            clearInterval(gg);
        }
    },500);
})();

(function(){
    var gg = setInterval(function() {
        if($('p:visible').length > 0) {
            $('p').trigger('click') ;
            clearInterval(gg);
        }
    },500);
})();

(These Functions are executed automatically...)

Upvotes: 1

luka
luka

Reputation: 327

in that other snippet change var gg =... to var hh =... and then clearInterval(hh)

Upvotes: 0

Related Questions