I enjoy timing puzzles, so I played with this one.
While the code is rather trivial, I don’t think that most users are fully aware of the JS timing functions , and if they are, they may not be patient enough to tinker with it.
I think the feature your requested should be built into the Wix/Corvid API.
The proper function to use for your desired effect is:
setTimeout (function, milliseconds);
Executes a function, after waiting a specified number of milliseconds.
I let you figure that one out, it’s rather easy.
To demonstrate and visualize the countdown I used this function:
setInterval (function, milliseconds);
Same as setTimeout(), but repeats the execution of the function continuously every x milliseconds, until you turn the timer off with code.
Sample page (it will be up for a while, but may be removed at my discretion):
https://www.ualmatrix.com/hoverbox
Scroll up and down - every time the Hoverbox enters the viewport, the countdown starts over.
Because the Hoverbox is hidden on load, it cannot trigger the onviewport event.
So, you overlay a blank and transparent image over the hoverbox and you let that trigger the onviewport event. Once the Hoverbox animation completes, you hide the blank image so that mouse-over effect on the Hoverbox can trigger. When the Hoverbox scrolls out of sight, you reset everything.
BTW, as @givemeawhiskey mentioned, images inside the Hoverbox can be animated with effects.
In my sample, mouse-over the Hoverbox for a few seconds, and you’ll see an image slide in.
Here is the code:
// Delay for Hoverbox with countdown before Hoverbox animation starts
var countDownSeconds = 7; // set desired countdown seconds (delay before Hoverbox shows up and animates with effect)
var myVar =0; // countdown interval timer
$w.onReady( function() {
//
} );
// blank tranparent image overlay over the Hoverbox to trigger the _viewportenter function
// since Hoverbox is hidden on page load, it cannot trigger the _viewportenter function by itself
export function imageBlankTransparent_viewportEnter(event) {
myVar = setInterval(timer, 1000); // start countdown timer: run timer() every 1000 milliseconds
}
function timer() {
countDownSeconds --;
$w("#textCounter").text = "" + countDownSeconds;
if ( countDownSeconds === 0) {
// finished countdown
// desired actions: show Hoverbox with animation (zero delay)
clearInterval(myVar); // end countdown timer
countDownSeconds = 7; // reset counter for next countdown
animateHoverbox() // start Hoverbox effect
$w('#imageBlankTransparent').hide(); // must hide to allow mouse-over of Hoverbox
}
}
export function animateHoverbox() { // delay must be set to zero for instant animation after countdown delay
//$w("#hoverBox1").hide()
$w('#hoverBox1').show('glide',{ "duration": 4000, "delay": 0, "angle": 180, "distance": 150 });
}
export function hoverBox1_viewportLeave(event) {
// unhide the transparent blank image that triggers the _viewportEnter(event)
// hide thenHoverbox so it's ready for a new countdown and effect when _viewportEnter(event) fires
$w('#imageBlankTransparent').show();
$w('#hoverBox1').hide();
}