在jQuery中使用setInterval调用函数? [英] Call function with setInterval in jQuery?

查看:168
本文介绍了在jQuery中使用setInterval调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在jQuery中创建对函数的间隔调用,但它不起作用!我的第一个问题是,我可以将常见的JavaScript与jQuery混合使用吗?

I'm trying to create a interval call to a function in jQuery, but it doesn't work! My first question is, can I mix common JavaScript with jQuery?

我应该使用 setInterval(test(),1000); 或类似的东西:

Should I use setInterval("test()",1000); or something like this:

var refreshId = setInterval(function(){
    code...
}, 5000);

我在哪里放置我调用的函数以及如何激活间隔?与jQuery相比,如何在JavaScript中声明一个函数是不同的?

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?

推荐答案

要编写最好的代码,你应该 使用后一种方法,带有函数引用:

To write the best code, you "should" use the latter approach, with a function reference:

var refreshId = setInterval(function() {}, 5000);

function test() {}
var refreshId = setInterval(test, 5000);

但是你接近

function test() {}
var refreshId = setInterval("test()", 5000);

基本上也是有效的(只要 test()是全局的。

is basically valid, too (as long as test() is global).

请注意,在jQuery中没有这样的东西。你还在编写Javascript语言;你只是使用了一些jQuery库的预制函数。

Note that there is no such thing really as "in jQuery". You're still writing the Javascript language; you're just using some pre-made functions that are the jQuery library.

这篇关于在jQuery中使用setInterval调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆