【问题标题】:Form: Onload a script, wait 2 sec, then load next表单:加载一个脚本,等待 2 秒,然后加载下一个
【发布时间】:2017-12-09 11:26:12
【问题描述】:

我有一个页面会自动获取位置,并将其填写到表单中。

</script>
<body onload="getLocation();">

然后我必须等待 2 秒,然后按下按钮将位置转换为真实地址。

<input id="submit" type="button" value="Reverse Geocode">

如何自动触发按钮或删除按钮并自动触发-

有2秒的延时?

代码:

 function getLocation(){

    if(navigator.geolocation){
       // timeout at 60000 milliseconds (60 seconds)
       var options = {timeout:60000};
       navigator.geolocation.getCurrentPosition(showLocation, errorHandler, options);
    }
    else{
       alert("Sorry, browser does not support geolocation!");
    }
 }

【问题讨论】:

  • 您是说要在页面加载 2 秒后单击按钮?

标签: html forms button


【解决方案1】:

您可以使用setTimeout() 函数在定义的毫秒数后运行函数。在你的情况下,

function getLocation(){
  if(navigator.geolocation){
    var options = {timeout:60000};
    navigator.geolocation.getCurrentPosition(showLocation, errorHandler, options);

    setTimeout(
      function(){ 
        document.getElementById("submit").click(); //Click the Button
      }, 
      2000 // 2000 milliseconds = 2 seconds
    );
  }

  else{
    alert("Sorry, browser does not support geolocation!");
  }
}

DOMclick() 方法用于通过 JavaScript 虚拟点击按钮。

【讨论】:

  • 所以我把它放在 - function getLocation(){ 和 if(navigator.geolocation){ ?
  • @Bergum 你想click()按钮在函数中的哪个位置?
  • @Bergum 如果对您有帮助,请接受我的回答(点击我的回答旁边的 tick
【解决方案2】:

您正在寻找“setTimeout”功能:

setTimeout(function () {
  // your code
}, 3000);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    相关资源
    最近更新 更多