在 jquery 移动设备中每 3 秒执行一次功能
Execute a function every 3 seconds in jquery mobile
我正在尝试在 jquery 移动设备中创建一个功能,在某个页面上每 3 秒自动刷新一次。
我试过:
$(document).on('pageshow', '#chat',function(){
function autoload(){
console.log('its after 3 sec')
}
autoload();
});
如何在 3 秒后将函数更改为 console.log('its after 3 sec') 即如何添加时间 interval.The 函数应该仅在页面上出现时执行(#聊天)
$(document).on('pageshow', '#chat',function(){
function autoload(){
console.log('its after 3 sec')
}
window.setInterval(autoload, 3 * 1000)
});
您可以使用setInterval方法,它会以所需的时间间隔(毫秒)执行指定的函数。
$(document).on('pageshow', '#chat', function() {
function autoload() {
console.log('its after 3 sec')
}
setInterval(autoload(), 3000);
});
要在隐藏页面时停止执行,您可以存储间隔 id 并使用 clearInterval 方法。
// store the interval id so we can clear it when the page is hidden
var intervalId;
$(document).on('pageshow', '#chat', function() {
function autoload() {
console.log('its after 3 sec')
}
intervalId = setInterval(autoload(), 3000);
});
$(document).on('pagehide', function() {
clearInterval(intervalId);
});
也可以使用setTimeout方法,类似于setInterval方法。
// store the timeout id so we can clear it when the page is hidden
var timeoutId;
$(document).on('pageshow', '#chat', function() {
function autoload() {
console.log('its after 3 sec')
timeoutId = setTimeout(autoload(), 3000);
}
autoload();
});
$(document).on('pagehide', function() {
clearTimeout(timeoutId);
});
我正在尝试在 jquery 移动设备中创建一个功能,在某个页面上每 3 秒自动刷新一次。
我试过:
$(document).on('pageshow', '#chat',function(){
function autoload(){
console.log('its after 3 sec')
}
autoload();
});
如何在 3 秒后将函数更改为 console.log('its after 3 sec') 即如何添加时间 interval.The 函数应该仅在页面上出现时执行(#聊天)
$(document).on('pageshow', '#chat',function(){
function autoload(){
console.log('its after 3 sec')
}
window.setInterval(autoload, 3 * 1000)
});
您可以使用setInterval方法,它会以所需的时间间隔(毫秒)执行指定的函数。
$(document).on('pageshow', '#chat', function() {
function autoload() {
console.log('its after 3 sec')
}
setInterval(autoload(), 3000);
});
要在隐藏页面时停止执行,您可以存储间隔 id 并使用 clearInterval 方法。
// store the interval id so we can clear it when the page is hidden
var intervalId;
$(document).on('pageshow', '#chat', function() {
function autoload() {
console.log('its after 3 sec')
}
intervalId = setInterval(autoload(), 3000);
});
$(document).on('pagehide', function() {
clearInterval(intervalId);
});
也可以使用setTimeout方法,类似于setInterval方法。
// store the timeout id so we can clear it when the page is hidden
var timeoutId;
$(document).on('pageshow', '#chat', function() {
function autoload() {
console.log('its after 3 sec')
timeoutId = setTimeout(autoload(), 3000);
}
autoload();
});
$(document).on('pagehide', function() {
clearTimeout(timeoutId);
});