Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TerrePorter's avatar

alpine blink effect

Hey all, I'm trying to alternate background images on a div using alpine to simulate a blinking effect.

{
    
    showBlueLightsBackground: false,
    
    pressSpinButton() {
                if (background_blink === null) {
                    console.log('blink trigger');
                    background_blink = setInterval(this.flashBackground, 1000);
                }


                setTimeout(() => {
                    console.log('blkink end')
                    clearInterval(background_blink);
                    this.showBlueLightsBackground = false;
                    console.log(this.showBlueLightsBackground);
                    background_blink = null;
                }, 5000);


            },

            flashBackground() {
                this.showBlueLightsBackground = !this.showBlueLightsBackground;

                console.log(this.showBlueLightsBackground);
            },
  }

test - https://codepen.io/terreporter/pen/XWQJOqL

Anyone know why this isn't working?

0 likes
4 replies
LaryAI's avatar
Level 58

It seems like you're trying to use Alpine.js to create a blinking effect by toggling a background image. From the code snippet you've provided, it looks like you're using a combination of setInterval and setTimeout to achieve this effect. However, without the full context of your Alpine.js component, it's a bit difficult to provide a precise solution. Nonetheless, I'll provide a general solution that should help you get the desired effect.

First, ensure that you have Alpine.js included in your project. Then, you can create an Alpine.js component with the necessary data and methods to handle the blinking effect.

Here's an example of how you might structure your Alpine.js component:

<div x-data="blinkingBackground()" x-bind:class="{'blue-background': showBlueLightsBackground}">
    <!-- Your content here -->
</div>

<script>
function blinkingBackground() {
    return {
        showBlueLightsBackground: false,
        backgroundBlink: null,

        pressSpinButton() {
            if (this.backgroundBlink === null) {
                console.log('blink trigger');
                this.backgroundBlink = setInterval(() => this.flashBackground(), 1000);
            }

            setTimeout(() => {
                console.log('blink end');
                clearInterval(this.backgroundBlink);
                this.showBlueLightsBackground = false;
                console.log(this.showBlueLightsBackground);
                this.backgroundBlink = null;
            }, 5000);
        },

        flashBackground() {
            this.showBlueLightsBackground = !this.showBlueLightsBackground;
            console.log(this.showBlueLightsBackground);
        },
    };
}
</script>

<style>
.blue-background {
    background-image: url('path-to-your-blue-lights-background.jpg');
}
</style>

In this example, the x-bind:class directive is used to toggle the class blue-background on the div based on the value of showBlueLightsBackground. The pressSpinButton method sets up an interval to call flashBackground every second, and a timeout to clear the interval after 5 seconds.

Make sure to replace 'path-to-your-blue-lights-background.jpg' with the actual path to your background image.

Also, ensure that the backgroundBlink variable is part of the component's data and not a global variable, as it seems to be in your original code snippet. This is important for the proper functioning of the Alpine.js component.

If you're still having trouble, please provide the full context of your Alpine.js component, including the HTML and any relevant CSS, so that I can give you a more accurate solution.

Snapey's avatar
Snapey
Best Answer
Level 122

This one runs automatically

https://codepen.io/snapey/pen/PogwLwe

<div x-data="
   {
     blue: false,
     init() { setInterval(() => this.blue = !this.blue, 1000)},
    }" >
 
  
  <div style="border:1px solid #cdcdcd; width:200px; height: 200px"
       
    :class="blue ? 'showBlueLightsBackground' : 'showYellowLightsBackground' ">
        
  </div>
</div>
TerrePorter's avatar

@Snapey Thanks, I had just noticed i had the setInterval using a function and not the () => and that fixed it.

Please or to participate in this conversation.