2016-11-11 60 views
0

我有下面的代碼它工作正常,但我想在5秒後清除innerHTML。不是每5秒一次,但當它出現時,它在5秒後再次被清除。一旦!5秒後清除innerHTML

function myfunction() { 
 
    var x = document.forms["forma"]["emri"].value; 
 
    var x1 = document.forms["forma"]["mbiemri"].value; 
 
    var x2 = document.forms["forma"]["email"].value; 
 
    var x3 = document.forms["forma"]["vendbanimi"].value; 
 
    if ((x == null || x == "") || (x1 == null || x1 == "") || (x2 == null || x2 == "") || (x3 == null || x3 == "")) { 
 
    document.getElementById("js").innerHTML = "Ju lutem ploresojini te gjitha te dhenat"; 
 
    return false; 
 
    } 
 
    if (x2.indexOf("@") < 1) { 
 
    document.getElementById("js").innerHTML = "Ju lutem insertoni nje email te sakte"; 
 
    return false; 
 
    } 
 
}
<div class="forma"> 
 
    <center> 
 
    <form class="form-orizontal" method="post" name="forma"> 
 
     <div class="input-prepend"> 
 
     <span class="add-on">e</span> 
 
     <input class="span2" id="prependedInput" type="text" placeholder="emri" name="emri"> 
 
     </div> 
 
     <br> 
 
     <div class="input-prepend"> 
 
     <span class="add-on">mb</span> 
 
     <input class="span2" id="prependedInput" type="text" placeholder="mbiemri" name="mbiemri"> 
 
     </div> 
 
     <br> 
 
     <div class="input-prepend"> 
 
     <span class="add-on">@</span> 
 
     <input class="span2" id="prependedInput" type="text" placeholder="email" name="email"> 
 
     </div> 
 
     <br> 
 
     <div class="input-prepend"> 
 
     <span class="add-on">vb</span> 
 
     <input class="span2" id="prependedInput" type="text" placeholder="vendbanimi" name="vendbanimi"> 
 
     </div> 
 
     <br> 
 
     <div class="input-prepend"> 
 
     <span class="add-on">V</span> 
 
     <select name="viti_lindjes" class="small"> 
 
      <option style="display:none">Viti i lindjes</option> 
 
      <option>1990</option> 
 
      <option>1991</option> 
 
      <option>1992</option> 
 
      <option>1993</option> 
 
      <option>1994</option> 
 
      <option>1995</option> 
 
      <option>1996</option> 
 
      <option>1997</option> 
 
     </select> 
 
     </div> 
 
     <div class="control-group"> 
 
     <div class="controls"> 
 
      <button type="submit" class="btn" name="submit" onclick="return myfunction()">Submit</button> 
 
      <input type="reset" class="btn"> 
 
     </div> 
 
     </div> 
 
     <div id="js"></div> 
 
    </form> 
 
    </center> 
 
</div>

+0

您需要向我們展示的是調用'myfunction的代碼()'。 – Blazemonger

+0

現在沒問題嗎?以上您有:html代碼 –

回答

-1

setTimeOut function waits for you

function appearing(){ 
 
    document.getElementById("inner").innerHTML = "Paragraph changed!"; 
 
    setTimeout(function(){ 
 
     document.getElementById("inner").innerHTML = ""; 
 
    }, 5000); 
 
}
<a href='#' onClick='appearing()'>Appear and disapper after 5 seconds</a> 
 
<div id='inner'></div>

+0

是的,我想消失的存在出現innerHTML,幾秒鐘後不出現 –

+0

@roliroli我在做什麼我的答案? – oguzhancerit

+0

看看我的代碼carefuly ..你發佈的代碼出現我的innerHTML內容5秒後..好吧,我有一個提交按鈕,當我按它出現innerHTML內容...我想,當它出現時,5秒後dissapear.did你明白 –

0

你要使用setTimeout()功能。

此代碼應該在你的情況下工作:

function myfunction() { 
    var x = document.forms["forma"]["emri"].value; 
    var x1 = document.forms["forma"]["mbiemri"].value; 
    var x2 = document.forms["forma"]["email"].value; 
    var x3 = document.forms["forma"]["vendbanimi"].value; 
    if ((x == null || x == "") || (x1 == null || x1 == "") || (x2 == null || x2 == "") || (x3 == null || x3 == "")) { 
    document.getElementById("js").innerHTML = "Ju lutem ploresojini te gjitha te dhenat"; 
    return false; 
    } 
    if (x2.indexOf("@") < 1) { 
     document.getElementById("js").innerHTML = "Ju lutem insertoni nje email te sakte"; 
    return false; 
    } 
    setTimeout(functionToDisappearInnerHTML(), 5000); //Sets a timer that starts the function in 5 seconds (5000 milliseconds) 
} 

function functionToDisappearInnerHTML() { 
    document.getElementById("js").innerHTML = ""; //Clears the innerHTML 
} 
+0

已更新的答案。感謝您指出了這一點。 –

+0

它不起作用 –

+0

當你運行它會發生什麼? –