0

Is, is there any function which does the exact same thing as alert except doesn't bring up an alert box? (a function which halts ALL executing JS code for a given amount of time, but doesn't bring up an alert message).

setTimeout doesn't work since it only halts the cod which is inside the setTimeout function.

user2817200
  • 1,097
  • 2
  • 18
  • 38
  • 3
    In what way does it "not work" when you remove the alert? – Niet the Dark Absol Nov 08 '13 at 17:04
  • when/where is created the element `'.'+test`? – Einacio Nov 08 '13 at 17:05
  • 2
    I think you're misinterpretating the problem. You shouldn't have to pause the code to make it work. You should try to find why it doesn't work, and re-think the code to make sure everything is done before the next line of code is read. – Romain Braun Nov 08 '13 at 17:06
  • Try using a [$(document).ready](http://api.jquery.com/ready/) to wrap the javascript code so the html can load before your js runs – megawac Nov 08 '13 at 17:06
  • The `Thread.sleep()` kind of approach is almost always the worst way of dealing with timing problems. At best, you slow down your application, but typically you end up making the race condition harder to reproduce. – Ingo Bürk Nov 08 '13 at 17:06
  • @RomainBraun right, that is one solution but there is a huge script and the alert makes it work the way I want it to work (except it brings up an alert box) which is why I'm wondering if there is something which does what alert does except doesn't bring up an alert box. – user2817200 Nov 08 '13 at 17:08
  • @NiettheDarkAbsol the code is supposed to generate a table and the td's of the table need to be a certain gradient (in my code, I have a 'if IE10 then make it this gradient, otherwise make it that gradient', and if you hover over the td's, they need to turn another gradient color. If I remove the alert, the td's do not have the gradient. – user2817200 Nov 08 '13 at 17:11
  • Why not just use CSS and `:hover`? – Niet the Dark Absol Nov 08 '13 at 17:13
  • 2
    It's not just "one solution" - it's the correct one. There is obviously a problem with your code and fixing that is the right thing to do. What you're talking about is masking the problem, which will then always be there. – Reinstate Monica Cellio Nov 08 '13 at 17:16
  • @Archer right, I want to mask the problem, not fix it. My job right now is to just make the code work, not read through the entire code and figure out what the problem is. And alert fixes the code except brings up a box.. is there any method or function which does exactly what alert does but doesn't bring up a box? – user2817200 Nov 08 '13 at 17:18
  • Try this kink. It may help you. http://stackoverflow.com/questions/6807799/stop-page-execution-like-the-alert-function – Raj Bahadur Singh Nov 08 '13 at 17:18

5 Answers5

0

a javascript function that will stop your code for some time is setTimeout(), for more information on how to use this function please reffer to the following link : http://www.w3schools.com/jsref/met_win_settimeout.asp

Gunnit
  • 1,064
  • 5
  • 22
  • 45
  • 1
    You are not solving a problem, you are making it worse. The only good way to deal with timing issues is to deal with the source of it, not by delaying execution and hoping you are waiting long enough. – Ingo Bürk Nov 08 '13 at 17:10
  • @ingoBurk the question is " I am wondering if there is something which stops the JS code for a certain amount of time," – Gunnit Nov 08 '13 at 17:13
  • @Gunnit right but I need something which halts all JS code, not just the certain JS code which is inside the setTimeout function. Alert halts all JS code, so I need something like alert but which doesn't bring up a box. – user2817200 Nov 08 '13 at 17:15
  • @Gunnit That might be, but we know the underlying problem and the OP is clearly asking the wrong question. Helping people to write horrible software is not really a great idea. – Ingo Bürk Nov 08 '13 at 17:17
  • I had to re-read the previous comment several times before believing it. "The OP is clearly asking the wrong question"? Are you serious? Give your head a shake. The question is valid - it appears you do not know the answer. (BTW, the answer is "no". Is that so hard?) – DaveWalley Jul 18 '14 at 21:42
0

You could split it into two functions and have the first initiate a time-out.

function func1(){
 //do stuff
 setTimeout('func2',2000);
}
function func2(){
 //do some more
}
0

The function setTimeout will execute for later whatever function you pass to it. So it will not really pause the execution of your code, unless the code is split into parts and placed within calls of setTimeout. Maybe that is an approach. Another approach could be to use delay function (http://api.jquery.com/delay/) along your jquery calls. But in all cases the best is to find out what is causing this behaviour and fix it in code.

melc
  • 11,523
  • 3
  • 36
  • 41
0

Have you tried using the DOM elements rather than JQuery to create the new node. http://www.w3schools.com/jsref/met_node_appendchild.asp

0
var startTime = new Date().getTime();
while(new Date().getTime() < startTime + 1000) {}

This waits for 1000ms / 1 sec. During that time, your browser is completely unresponsive and possibly doesn't render any stuff you may be waiting for, but hey, ... there you go ;)

Squ1sh
  • 27
  • 3