2015-09-08 10 views
7

Il mio codice è:Come restituire il testo precedente dopo una funzione JS onclick?

function changeText() 
{ 
document.getElementById('button').innerHTML = 'Downloading...'; 
} 

Il pulsante:

<button id = "button" onclick='changeText()' value='Change Text'>Download file as CSV</button> 

voglio il pulsante per passare a "Download ..." per poi tornare a "Scarica come CSV" pochi secondi dopo, è questo possibile in JS?

risposta

5

È possibile utilizzare setTimeout o in caso di successo della vostra chiamata asincrona:

function changeText() { 
 
    document.getElementById('button').innerHTML = 'Downloading...'; 
 
    setTimeout(previousText, 1000); 
 
} 
 

 
function previousText() { 
 
    document.getElementById('button').innerHTML = 'Download file as CSV'; 
 
}
<button id="button" onclick='changeText()' value='Change Text'>Download file as CSV</button>

+0

Questo funziona brillantemente, grazie mille. – Sean9113

+0

@ Sean9113 Felice di averti aiutato :) –

4
document.getElementById("your-button").value="Downloading..."; 
setTimeout(function() { 
    document.getElementById("your-button").value="Download as CSV"; 
}, 3000); 

Il codice sopra imposta il testo del pulsante, e quindi attende 3 secondi e dopo che imposta il testo del pulsante di un'altra stringa.

+0

Brillante, grazie mille! – Sean9113

1

Questo codice sarà ripristinare il vecchio testo dopo 2000 ms, ma è possibile personalizzare il timeout.

function changeText() { 
    var button = document.getElementById('button'); 

    var oldText = button.innerHTML; 
    button.innerHTML = 'Downloading...'; 
    setTimeout(function(){ 
     button.innerHTML = oldText; 
    }, 2000); 
} 
Problemi correlati