Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 524
Shaun Avatar asked Apr 14 '26 08:04

Shaun


1 Answers

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.

like image 98
undefined Avatar answered Apr 16 '26 21:04

undefined



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!