user2533777
user2533777

Reputation:

Jquery delay function not working

I have an password field and an small icon on right and I want to show the inputted password for 2 sec when user click on the icon. What I have done is.

$("#showpass").click(function(){
   $('#pass').attr('type','text').delay(2000).attr('type','password');
);

It does nothing but when I only do $('#pass').attr('type','text') it works without any issue.

Upvotes: 0

Views: 68

Answers (2)

jfriend00
jfriend00

Reputation: 707288

Delay only works with functions that use the jQuery queue such as animations, thus you are getting no delay between setting the type to text and type back to password.

Your particular issue can be solved with setTimeout().

$("#showpass").click(function(){
   var pass = $('#pass').attr('type','text');
   setTimeout(function() {
       pass.attr('type','password');
   }, 2000);
);

Upvotes: 1

ProllyGeek
ProllyGeek

Reputation: 15836

The .delay() method is best for delaying between queued jQuery effects. Because it is limited—it doesn't, for example, offer a way to cancel the delay—.delay() is not a replacement for JavaScript's native setTimeout function, which may be more appropriate for certain use cases.

better use it only for animation issues :

https://api.jquery.com/delay/

use setTimeout instead.

Upvotes: 1

Related Questions