Shaun
Shaun

Reputation: 2181

Any reason why this javascript blink code won't work?

I have the below code in the second of two js files from a web-app.

It works fine, until I combine the two js files into one. Then the js breaks.

function oBlink()
{ 
return window.setInterval
( 
    function()
    {
        $("#sOr").css("background-color", function (){ this.switch = !this.switch; return this.switch ? "#F90" : ""  });    
    }
    , 500 
);
}

I've isolated the problem to the code

this.switch = !this.switch; return this.switch ? "#F90" : ""

If I take that out, the rest of my js works fine.

I understand there are a lot of external variables that could be coming into play here, but I just wanted to check with you guys that the above function code doesn't have any errors in it.

Thanks for taking a look.

Upvotes: 3

Views: 109

Answers (2)

Ram
Ram

Reputation: 144689

it's working fine in the browser, but failing when checking it on certain devices in the Android emulator.

That's probably because you are using switch in your code which is a reserved word in JavaScript. Only ECMAScript5-based browsers allow using reserved words as object's properties.

Instead of using a flag you can declare a CSS class and use the jQuery's toggleClass method.

Upvotes: 3

amey91
amey91

Reputation: 552

Make sure you define somewhere

switch = false

Then Try

$("#sOr").css("background-color", function (){ this.switch = !this.switch; return (this.switch ? "#F90" : "#FFF" ) });

Upvotes: -2

Related Questions