Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
651 views
in Technique[技术] by (71.8m points)

javascript - Display a page loading gif after clicking form submit

I have a simple UI like below: enter image description here

when clicks on submit, it redirects to another route/url. But it takes a little bit time to load the next page cause when I click the submit button, in the backed, it runs few machine learning models.

Now, during this loading and processing time, I want to display a page loading GIF. During the display of page loading, I want my form to fade away and only display the GIF in the middle of the page.

I have tried different ways to do it, but seems it's not working perfectly.

The form submission code:

<form class="form-signin" id="myform" action="data" method="post" enctype="multipart/form-data">
        <p class="form-signin-heading">upload input sample in csv format to run the model inference</p>

        <input class="form-control-file" type="file" name="upload-file" value="">
        <input class="form-control-file" type="submit" name="" value="Submit">
</form>

Any help?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This cam be a starting point. When the submit button is clicked, the form will fade out completely, and the spinner will appear. Once your backend is done processing, a redirect will load a the new page.

const form = document.getElementById('myform');
const spinner = document.getElementById('spinner');

form.addEventListener('submit', (e) => {
  e.preventDefault();
  form.classList.toggle('fade-out');
  spinner.classList.toggle('show');
});
form{
  text-align: center;
  transition: opacity 0.6s ease-out;
}

img{
  display: none;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.show{
  display: block;
}

.fade-out{
  opacity: 0;
}
<form class="form-signin" id="myform" action="data" method="post" enctype="multipart/form-data">
    <p class="form-signin-heading">upload input sample in csv format to run the model inference</p>

    <input class="form-control-file" type="file" name="upload-file" value="">
    <input class="form-control-file" type="submit" name="" value="Submit">
</form>
<img src="https://www.dariusland.com/images/load.gif" id="spinner" class="hide">

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...