strange error.

Z

ziobudda

Hi all. I have this code:

<html>
<head>
<script language="javascript" type="text/javascript">
function sleep(n)
{
var now = new Date();
var exitTime = now.getTime() + (n*1000);
while (true) {
now = new Date();
if (now.getTime() > exitTime) return;
}
}

function mostra() {
tmp = document.createElement('input');
tmp.setAttribute('type','text');
tmp2= document.getElementById('miodiv');
tmp2.appendChild(tmp);
//Only for try
tmp2.innerHTML = tmp2.innerHTML;
sleep(1);
}

</script>
</head>
<body>
<a href="#" onclick="mostra()">Clicca</a>
<div id="miodiv" style=""></div>
</body>
</html>

Ok ? I need to insert something into "miodiv" (an input type in this
case) and stop javascript for 1 second.

Now my problem: the "input type" is write into "miodiv" only after that
the sleep() function is terminated.

I don't know why.

Tnx.

PS: anyone have a sleep() function that works ?

M.
 
J

Jonas Raoni

ziobudda escreveu:
Hi all. I have this code:

Falae Bunda xD
<script language="javascript" type="text/javascript">

The language attribute isn't needed ;]
function sleep(n)
{
var now = new Date();
var exitTime = now.getTime() + (n*1000);
while (true) {
now = new Date();
if (now.getTime() > exitTime) return;
}
}

This is one of the worst things you can do in a language that is
processed in the client :]
<a href="#" onclick="mostra()">Clicca</a>

A phantom link said:
Ok ? I need to insert something into "miodiv" (an input type in this
case) and stop javascript for 1 second.

Why?

You should use the setTimeout to achieve such delays.

setTimeout(function(){
alert("Then, after one second, pedala robinho xD");
}, 1000);

Now my problem: the "input type" is write into "miodiv" only after that
the sleep() function is terminated.

You made the browser sleep :)
PS: anyone have a sleep() function that works ?

Your one works.
 
Y

yb

I'd recommend looking into window methods setTimeout / clearTimeout, or
setInterval / clearInterval.

A "sleep" function shouldn't be necessary. You don't want to "freeze"
the browser like this.
 
E

Evertjan.

ziobudda wrote on 28 dec 2006 in comp.lang.javascript:
Hi all. I have this code:

<html>
<head>
<script language="javascript" type="text/javascript">
function sleep(n)
{
var now = new Date();
var exitTime = now.getTime() + (n*1000);
while (true) {
now = new Date();
if (now.getTime() > exitTime) return;
}
}
PS: anyone have a sleep() function that works ?

If you had read up on a javascript sleep function, you would know it
would not do to take over 100% of the allotted cpu time for such function
in any multitasking environment.

NEVER try a sleep function in zsuch environments, but use a kernetl
timout function:

var timeoutPointer = setTimeout('nextFunction()',milliseconds)

This needs you to think in a modular programming concept,
lot the old linear one from the single task assambler language, where a
looping timeout was ok.
function mostra() {
tmp = document.createElement('input');
tmp.setAttribute('type','text');
tmp2= document.getElementById('miodiv');
tmp2.appendChild(tmp);
//Only for try
tmp2.innerHTML = tmp2.innerHTML;

you mean something like:
tmp2.innerHTML = tmp.value;
???
sleep(1);

I have on idea why you should wait here,
and how you could see that you waited!!!

[please do not use tabs in usenet code texts]

Try, subject to my understanding of what you would have wanted:

================ test.html ==========

<script type='text/javascript'>

function doIt() {
var myDiv= document.getElementById('myDiv');
var theInput = document.getElementById('theInput');
myDiv.innerHTML = theInput.value;
};

function mostra() {
var myDiv= document.getElementById('myDiv');
myDiv.innerHTML = 'Please wait ......';
setTimeout('doIt()',1200)
}

</script>

<a href="#" onclick="mostra()">Clicca/click</a>
<br><br>
<input type='text' id='theInput' value='change this text'>
<br><br>
<div id="myDiv">-----------</div>

====================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top