最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How to display a message for a few seconds and then disappear in javascript - Stack Overflow

programmeradmin1浏览0评论

The following is a code snippet for successful username entry. It checks the value from the database and displays message accordingly. I want to display the else part only for a few seconds i.e. "Username successful" message should show and disappear.

if(xmlhttp.responseText==1)
{
    document.getElementById("check").innerHTML="Username already exists";
}
else
{
    document.getElementById("check").innerHTML="Username successfull";          
}

The check class is as follows:

<ul class="user">
    <li class="label">User name </li>
    <li class="field">
        <input type="text" id="username" name="username" class="required error" onchange="checkuser(this.value);"/> 
       <div class="check " id="check"/>
    </li>
</ul>

The following is a code snippet for successful username entry. It checks the value from the database and displays message accordingly. I want to display the else part only for a few seconds i.e. "Username successful" message should show and disappear.

if(xmlhttp.responseText==1)
{
    document.getElementById("check").innerHTML="Username already exists";
}
else
{
    document.getElementById("check").innerHTML="Username successfull";          
}

The check class is as follows:

<ul class="user">
    <li class="label">User name </li>
    <li class="field">
        <input type="text" id="username" name="username" class="required error" onchange="checkuser(this.value);"/> 
       <div class="check " id="check"/>
    </li>
</ul>
Share Improve this question edited Mar 26, 2014 at 8:40 Martin Prikryl 203k64 gold badges545 silver badges1.1k bronze badges asked Mar 26, 2014 at 8:24 user3463147user3463147 451 gold badge1 silver badge3 bronze badges 1
  • 1 Use setTimeout to get innerHTML back to an empty string. – Niet the Dark Absol Commented Mar 26, 2014 at 8:25
Add a ment  | 

1 Answer 1

Reset to default 7

You can use setTimeout so that after a period of time you want to change Usernane successfull to an emptry string

Try:

if(xmlhttp.responseText==1)
        {
          document.getElementById("check").innerHTML="Username already exists";
        }
        else
        {
         document.getElementById("check").innerHTML="Username successfull";
         //ADD THIS CODE
         setTimeout(function(){
         document.getElementById("check").innerHTML="";
         },3000);

        }
    }

Or you can change the display attribute after period of time you want again with setTimeout

Try:

if(xmlhttp.responseText==1)
            {
              document.getElementById("check").innerHTML="Username already exists";
            }
            else
            {
             document.getElementById("check").innerHTML="Username successfull";
             //ADD THIS CODE
             setTimeout(function(){
             document.getElementById('check').style.display = "none";
             },3000);

            }
        }
发布评论

评论列表(0)

  1. 暂无评论