jQuery setIntervel 直到条件为真
jQuery setIntervel until condition is true
页面加载后,我喜欢每 1 秒检查一次条件,直到它变为真,然后终止该函数。我尝试了以下脚本,该脚本简单地将重负载放在页面启动上。
$(document).ready(function () {
setInterval(function () {
for (var i = 0; i < 1; ) {
if ($(".showPrice").length) {
console.log("yes");
i = 1;
} else {
return false;
}
}
}, 1000);
});
为了能够清除interval you need a reference to the interval and pass it to clearInterval()
,只需将其赋值给一个变量:
$(document).ready(function () {
let interval = setInterval(function () {
if ($(".showPrice").length) {
clearInterval(interval);
console.log('Cleared!');
}
}, 1000);
setTimeout(function(){
$('.placeholder').html('<div class="showPrice">11.00 $</div>');
}, 3000)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<div class="placeholder"></div>
</body>
页面加载后,我喜欢每 1 秒检查一次条件,直到它变为真,然后终止该函数。我尝试了以下脚本,该脚本简单地将重负载放在页面启动上。
$(document).ready(function () {
setInterval(function () {
for (var i = 0; i < 1; ) {
if ($(".showPrice").length) {
console.log("yes");
i = 1;
} else {
return false;
}
}
}, 1000);
});
为了能够清除interval you need a reference to the interval and pass it to clearInterval()
,只需将其赋值给一个变量:
$(document).ready(function () {
let interval = setInterval(function () {
if ($(".showPrice").length) {
clearInterval(interval);
console.log('Cleared!');
}
}, 1000);
setTimeout(function(){
$('.placeholder').html('<div class="showPrice">11.00 $</div>');
}, 3000)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<div class="placeholder"></div>
</body>