Wait until a condition is true?

前端 未结 7 1967
日久生厌
日久生厌 2020-12-09 08:32

I\'m using navigator.geolocation.watchPosition in JavaScript, and I want a way to deal with the possibility that the user might submit a form relying on locatio

7条回答
  •  情书的邮戳
    2020-12-09 09:20

    You'll want to use setTimeout:

    function checkAndSubmit(form) {
        var location = getLocation();
        if (!location) {
            setTimeout(checkAndSubmit, 500, form); // setTimeout(func, timeMS, params...)
        } else {
            // Set location on form here if it isn't in getLocation()
            form.submit();
        }
    }
    

    ... where getLocation looks up your location.

提交回复
热议问题