jQuery: I am struggling to make my blinking text visible once the blinking animation has ended

While working on a text-blinking code, I discovered that under certain timing conditions, the following code can cause the text to remain hidden even after the animation has stopped:

//Blink settings
var blink = {
    obj: $(".blink"),
    timeout: 15000,
    speed: 1000
};

//Start function
blink.fn = setInterval(function () {
    blink.obj.fadeToggle(blink.speed);
}, blink.speed + 100);
//End blink animation, after 'blink.timeout' milliseconds
setTimeout(function () {
    clearInterval(blink.fn);
    blink = null;
}, blink.timeout);

View fiddle here

Attempts were made to resolve this issue by adding blink.obj.show(), blink.obj.css(...) etc. immediately after clearInterval(blink.fn);, however, it was not successful.

...
setTimeout(function () {
    clearInterval(blink.fn);
    blink.obj.show();
    blink.obj.css("visibility","visible");
...

My goal is to ensure that the text is ALWAYS visible once the blinking stops

Answer №1

After the blink effect is finished, try using blink.obj.fadeIn().

If you want to maintain the same speed, use blink.obj.fadeIn(blink.speed):

...custom code...
//Stop blinking animation after 'blink.timeout' milliseconds
setTimeout(function () {
    clearInterval(blink.fn);
    //To solve your issue, add the line below
    blink.obj.fadeIn(blink.speed);
    blink = null;
}, blink.timeout);
...

To see the updated jsfiddle, click here: http://jsfiddle.net/jormaechea/mksh4eu3/7/

Answer №2

Your code looks good, but you should remove the +100 that was added. Instead, try tweaking the timing slightly without adding 100, as 15000 is already less than that amount when it reaches there.

//Blink settings
var blink = {
  obj: $(".blink"),
  timeout: 15000,
  speed: 1000
};

//Start function
blink.fn = setInterval(function () {
  blink.obj.fadeToggle(blink.speed);
}, blink.speed+100);
//End blink animation, after 'blink.timeout' milliseconds
setTimeout(function () {
  clearInterval(blink.fn);
  blink.obj.fadeIn(blink.speed+1);
  blink = null;
}, blink.timeout);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="blink">Flashing Text</div>

EDIT 2: To make sure everything works smoothly, consider adjusting the timing by just one millisecond in both

blink.obj.fadeToggle(blink.speed);
and blink.obj.fadeIn(blink.speed+1); within your stop function.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Improved AJAX Dependency

A few days ago, a question was posted with messy code and other issues due to my lack of experience (please forgive the form handling as well). However, I have made some improvements and added context. The main problem now lies in the second AJAX call. Ch ...

Vuetify Autocomplete that allows for adding values not in the predefined list

I am utilizing a vuetify autocomplete component to showcase a list of names for users to select from. In the case where a user enters a name not on the list, I want to ensure that value is still accepted. Check out my code snippet below: <v-autocomplete ...

Accessing factory in controller using AngularJS

Currently, I am utilizing the following link to integrate requirejs with angularjs: https://github.com/StarterSquad/startersquad.github.com/tree/master/examples/angularjs-requirejs-2 My question is regarding how to use a service function that is defined ...

Inject parameters into the templateUrl property of an Angular component

In this scenario, my component object is structured as follows: var options = { bindings: { title: '<', rows: '<' }, controller: registers, templateUrl: function ($element, $attrs) { return ...

Converting JavaScript table input values to JSON format

I need help saving the value in a table's input as JSON. I tried using serializeArray but it only retrieves 2 data points when I need all 3 (id and value). HTML Code: This is what I have tried: var myRows = { myRows: [] }; var $th = $('table t ...

The Yeoman Angular Coffee router is not routing properly and displays an error message "cannot GET"

Struggling with getting the router to load a basic template after setting up a Yeoman angular scaffolder installation. Here's my configuration: // index.html <body ng-app="mvmdApp"> <div class="container" ng-view=""></div>// not ...

Error Handling with Node.js Sequelize RangeError

Currently, I am in the process of setting up a table to store user sessions. Specifically, I plan to save the IP address as an integer and have been exploring various methods for achieving this. You can find more information on this topic here: IP-addresse ...

Tips for detecting if text appears in bold on a webpage using Selenium

I came across a pdf document with the following content: <div class=**"cs6C976429"** style="width:671px;height:18px;line-height:17px;margin-top:98px;margin-left:94px;position:absolute;text-align:left;vertical-align:top;"> <nobr ...

The fluid jumbotron in react-bootstrap is not extending to the entire width of the webpage

For the past few days, I've been engrossed in working on a website to sharpen my React skills using create-react-app, react-bootstrap, and styled-components. Initially, everything was smooth sailing with the jumbotron when it resided in my App.js file ...

Ensure one div scrolls independently while the other remains fixed using Bootstrap

I am currently in the process of constructing a web page with Bootstrap 4. The layout consists of two columns, each contained within individual divs. My requirement is for the content on the right side to be scrollable while the content on the left remains ...

Showing content based on the current date using Javascript

Although I may not be proficient in Javascript, I was able to gather examples and piece together the following code: var date = new Date().getDate(); var greeting; if (date < 24) { greeting = "Nej det är:"; } else { ...

Performing a fetch() POST request in Express.js results in an empty body object being generated

Purpose: Implement a function in fetch() to send specified string data from the HTML document, for example "MY DATA" Here is the code snippet: HTML Code: <!DOCTYPE html> <html> <body> <script type="text/javascript"> function ...

Choose the present date as the default in the Bootstrap calendar

There will be a compact form on the page along with a user-friendly bootstrap calendar for selecting dates. To display the selected date on the button instead of the default text, simply click on the "click" button after selecting a date. If you want to ...

Dynamic RSS Aggregator

I'm interested in developing a simple application that can retrieve RSS feeds from any feed URL. Can anyone provide me with some basic guidance on how to accomplish this? I'm fairly new to AJAX and similar technologies, so any assistance would b ...

I created a custom discord.js-commando command to announce all the channels that my bot is currently active in, however, encountered an unexpected error

const Commando = require('discord.js-commando'); module.exports = class AnnounceCommand extends Commando.Command { constructor(client) { super(client, { name: 'announce', aliases: ['an'], ...

Using Vue to input an array

I'm struggling with how to handle this issue. The task involves taking input which should be a URL, and the user should be able to enter multiple URLs. For instance: <input type="text" v-model="fields.urls" class=&quo ...

Google information window: clicking a link will trigger a page refresh

I am currently running some tests for a web page that I need to design. As part of this process, I have written some code to add markers on a map and include an Info Window for each marker with specific information: function placeLocationMarker(latitude, ...

Determine if an option is chosen in multiple select elements using Vanilla JavaScript

In order to determine if a checkbox is checked, we use the following code: let isChecked = event.target.checked But what about multiple select options like the example below? <select name="books[]" multiple> <option value="A">A</option& ...

Tips for positioning a box on top of a map in a floating manner

I am trying to figure out how to position the div with class box on top of the map in this jsbin. Can someone help me achieve this? Below is the CSS code I have for styling the box and body. body { font-family: "Helvetica Neue", Helvetica, sans-serif; ...

Issue with JQuery: Inability to deactivate an element after receiving an Ajax response

My dynamic dialogue box, generated via Ajax return, presents a challenge involving the dynamically changing drop-down list element $('#functionSelect'). I require this list to trigger disabling of input fields within the dialogue box upon changes ...