简体   繁体   中英

Javascript setTimeout Function Keeps Running

I have an index page for a web app that does a couple of things. First, it geocodes the user's location and stores it in a variable (ie the javascript variable variableToSend below). Second, it sends that data to mySQL via a php script after 10 seconds (to give the client side time to capture the location).

Everything is working great with one exception - the setTimeout function that I'm using keeps running every 10 seconds and I need to run once and only once on that page.

Here's the code:

 <script language="JavaScript"> setTimeout(function(){ var variableToSend = document.getElementById("latLng"); document.forms[0].submit(); $.post('html/index.php', {$latLong: "variableToSend"}); //window.clearInterval(interval); }, 10000); </script> 

Any help is greatly appreciated - let me know if you need more info.

它可以帮助您点击链接删除settimeout

<script language="JavaScript">
         var t = setTimeout(function(){
             //do stuff
        }, 10000);

        // sometime later in your code
        clearTimeout(t);
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM