[英]Call function with setInterval in jQuery?
I'm trying to create a interval call to a function in jQuery, but it doesn't work!我正在尝试在 jQuery 中创建对函数的间隔调用,但它不起作用! My first question is, can I mix common JavaScript with jQuery?
我的第一个问题是,我可以将常见的 JavaScript 与 jQuery 混合使用吗?
Should I use setInterval("test()",1000);
我应该使用
setInterval("test()",1000);
or something like this:或类似的东西:
var refreshId = setInterval(function(){
code...
}, 5000);
Where do I put the function that I call and how do I activate the interval?我在哪里放置我调用的函数以及如何激活间隔? Is it a difference in how to declare a function in JavaScript compared to jQuery?
与 jQuery 相比,如何在 JavaScript 中声明函数有区别吗?
To write the best code, you "should" use the latter approach, with a function reference:要编写最好的代码,您“应该”使用后一种方法,并带有函数引用:
var refreshId = setInterval(function() {}, 5000);
or或者
function test() {}
var refreshId = setInterval(test, 5000);
but your approach of但你的方法
function test() {}
var refreshId = setInterval("test()", 5000);
is basically valid, too (as long as test()
is global).基本上也是有效的(只要
test()
是全局的)。
Note that there is no such thing really as "in jQuery".请注意,实际上没有“在 jQuery 中”这样的东西。 You're still writing the Javascript language;
您仍在编写 Javascript 语言; you're just using some pre-made functions that are the jQuery library.
你只是在使用一些 jQuery 库的预制函数。
First of all: Yes you can mix jQuery with common JS :)首先:是的,您可以将 jQuery 与普通 JS 混合使用 :)
Best way to build up an intervall call of a function is to use setTimeout methode:建立函数的间隔调用的最佳方法是使用 setTimeout 方法:
For example, if you have a function called test() and want to repeat it all 5 seconds, you could build it up like this:例如,如果您有一个名为 test() 的函数并希望将其全部重复 5 秒,您可以像这样构建它:
function test(){
console.log('test called');
setTimeout(test, 5000);
}
Finally you have to trigger the function once:最后你必须触发一次函数:
$(document).ready(function(){
test();
});
This document ready function is called automatically, after all html is loaded.在加载所有 html 后,将自动调用此文档就绪函数。
jQuery is just a set of helpers/libraries written in Javascript. jQuery 只是一组用 Javascript 编写的帮助程序/库。 You can still use all Javascript features, so you can call whatever functions, also from inside jQuery callbacks.
您仍然可以使用所有 Javascript 功能,因此您可以调用任何函数,也可以从 jQuery 回调内部调用。 So both possibilities should be okay.
所以这两种可能性都应该没问题。
I have written a custom code for setInterval function which can also help我为 setInterval 函数编写了一个自定义代码,它也可以提供帮助
let interval; function startInterval(){ interval = setInterval(appendDateToBody, 1000); console.log(interval); } function appendDateToBody() { document.body.appendChild( document.createTextNode(new Date() + " ")); } function stopInterval() { clearInterval(interval); console.log(interval); }
<!DOCTYPE html> <html> <head> <title>setInterval</title> </head> <body> <input type="button" value="Stop" onclick="stopInterval();" /> <input type="button" value="Start" onclick="startInterval();" /> </body> </html>
setInterval(function() {
updatechat();
}, 2000);
function updatechat() {
alert('hello world');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.