0

I need to call spinner on click and it need to show up 2 seconds and then disappear.I cant use setTimeout and setInterval...is there any other function for that?

None
  • 8,817
  • 26
  • 96
  • 171
  • 5
    Might be an idea to explain why you can't use setTimeout or setInterval. – Jonathan Smith Jun 17 '15 at 10:48
  • How can i set that function to call it on click once and last 2 seconds...because i know that setTimeout function is something like delay and setInterval call function every X time...am i right? – None Jun 17 '15 at 10:50
  • I think it will help you .fadeOut() http://api.jquery.com/fadeout/ – Prasanna Jun 17 '15 at 10:52
  • @None You can call settimeout just once, see my answer. – Tech Savant Jun 17 '15 at 10:54
  • 2
    You show the spinner and then use `setTimeout` to remove it after 2 seconds. – Andy Jun 17 '15 at 10:55
  • Why did you accept the worst answer. You asked for native javascript and that is what I provided. If you just want to use setTimeout this is a dupe of a lot of other questions. – Tech Savant Jun 17 '15 at 11:21
  • possible duplicate of [Put a Delay in Javascript](http://stackoverflow.com/questions/1183872/put-a-delay-in-javascript) – Tech Savant Jun 17 '15 at 11:22
  • how can be possible of duplicate when i say that i dont want delay...so if u are mad thats your problem that u give -1 – None Jun 17 '15 at 11:30

3 Answers3

2

Ok, here's a quick example using setTimeout.

HTML

<button>Click me</button><br>
<div id="spinner">I am a spinner</div>

CSS

#spinner {
    display: none;
}

JavaScript

// pick up the elements to be used
var button = document.querySelector('button');
var spinner = document.querySelector('#spinner');

// add an event to the button so that `showThing` is run
// when it is clicked
button.onclick = showThing;

// `showThing` simply displays the spinner and then
// calls `removeThing` after 2 seconds
function showThing() {
  spinner.style.display = 'block';
  setTimeout(removeThing, 2000)
}

// and `removeThing` removes the spinner
function removeThing() {
  spinner.style.display = 'none';  
}

DEMO

Andy
  • 61,948
  • 13
  • 68
  • 95
1
function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

RE: Your comment about setTimeout()

To use setTimeout just once...

setTimeout(myfunction(), 2000);
Tech Savant
  • 3,686
  • 1
  • 19
  • 39
0

You could try setting up CSS animation to do this. An explanation of CSS animation can be found here:

https://css-tricks.com/almanac/properties/a/animation/

Or use a setTimeout like this:

Javascript:

$('#something').hide();
$('#clicky').on('click', function () {
   $('#something').show();
    setTimeout(function () {
        $('#something').hide();
    }, 2000);
});

HTML:

<button id="clicky">Click me</button>

<p id="something">Boo!</p>

http://jsfiddle.net/jonnyknowsbest/kkqqjz0v/

Jonathan Smith
  • 2,390
  • 1
  • 34
  • 60
  • Do NOT use CSS animations to do timeouts. The question was for a native javascript way. If this was just a question on how to use setTimeout it would be a dupe of 1000 other questions. – Tech Savant Jun 17 '15 at 11:21
  • Many apologies for giving options. The OP was for displaying a visual element for 2 seconds, and then making it disappear. I must have somehow missed the word "native" when i read the question. – Jonathan Smith Jun 17 '15 at 11:23
  • Yeah but he specifically said he didn't want setTimeout and CSS animations is not the best way. You can just write a sleep function using date. – Tech Savant Jun 17 '15 at 11:25
  • Can you identify for me where he said he didn't WANT to use setTimeout? – Jonathan Smith Jun 17 '15 at 11:26
  • OK, read it 4 times now. Definitely doesnt say "native", definitely does say "cant use". Just looking through the numerous question-edits now...... – Jonathan Smith Jun 17 '15 at 11:34
  • there are no edits to this question. now your just being silly. anyway man, if you see it differently, that's fine. i'm not here to teach you about reading comprehension, that's a different site – Tech Savant Jun 17 '15 at 11:43
  • Fair enough. I'll adopt your method of reading words that are clearly not there instead. I wont delay you any longer, you must have other accepted answers that are not yours to downvote. Keep up the good work! – Jonathan Smith Jun 17 '15 at 11:49