سؤال

I have

Myitem1.MyFunc();
Myitem2.MyFunc();

function MyFunc(){
  if (okToUpdate){
    //we weren't here from anywhere within the last 100 milliseconds
  }
}

Unfortunately, okToUpdate variable is updated not immediately, so it needs at least 100 milliseconds from the previous call of the function to have the correct value. How to make MyFunc be executed with the delay?

هل كانت مفيدة؟

المحلول

setTimeout(function(){
       Myitem1.MyFunc();
},100)

will do the trick. If you explain what you want exactly I can give you exact timing code.

نصائح أخرى

This code may help you

var clrTime=null;

MyFunc(){
    clrTime=setTimeout(function(){
        if (okToUpdate){
            // your code to do 
        }
    },100);
}

Using callbacks or promises would be the clean solution. Not some kind of busy waiting implemented with timeouts.

Have a look into publish/subscribe pattern.

I would use the setTimeout() function. Something like this:

<script>
function myFunction() {
    setTimeout(function(){MyFunc()},100);
}
</script>

<body onload="myFunction()">
</body>

setTimeout() method

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top