HTML:
<div id="coin1"></div>
Run Code Online (Sandbox Code Playgroud)
JS:
$(document).ready(function(){
function changeImage(){
if($("#coin1").css("display") == "none"){
$("#coin1").fadeIn("slow");
}else{
$("#coin1").fadeOut("slow");
}
};
setInterval ( "changeImage()", 2000 );
});
Run Code Online (Sandbox Code Playgroud)
我不能让这个工作...如果我只是做changeImage(); 它工作正常,但我想setInterval工作..任何想法?
我在setIntervel()中有一个函数,我想明确地停止执行这个函数.我怎么能这样做???函数是..
function autoLoad(chatboxtitle, msgId) {
setInterval(function() {
if (msgId != '') {
$.ajax({
type: "POST",
url: "../WebService1.asmx/GetMessagesDetail",
data: '{ "messageID": "' + msgId + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function(msg) {
$("#chatbox_" + chatboxtitle + ".chatboxcontent").empty();
$("#chatbox_" + chatboxtitle + ".chatboxcontent").append(msg.d);
$contentLoadTriggered = false;
},
error: function(x, e) {
alert("error");
}
});
}
}, 8000);
}
Run Code Online (Sandbox Code Playgroud) setInterval结果可以阻止页面中的其他脚本吗?
我正在开展一个将Gmail相关书签转换为Google Chrome扩展程序的项目.bookmarklet使用gmail greasemonkey API与gmail页面进行交互.API的JavaScript对象是要加载的Gmail页面的最后部分之一,并通过XMLHttpRequest加载到页面中.由于我需要访问此对象,并且从扩展内容脚本中隐藏了全局JavaScript变量,因此我将一个脚本注入gmail页面,该页面轮询变量的定义然后访问它.我正在使用setInterval函数进行轮询.这大约有80%的时间都有效.其余的时间轮询功能一直保持轮询,直到达到我设置的限制,并且页面中永远不会定义greasemonkey API对象.
注入的脚本示例:
var attemptCount = 0;
var attemptLoad = function(callback) {
if (typeof(gmonkey) != "undefined"){
clearInterval(intervalId); // unregister this function for interval execution.
gmonkey.load('1.0', function (gmail) {
self.gmail = gmail;
if (callback) { callback(); }
});
}
else {
attemptCount ++;
console.log("Gmonkey not yet loaded: " + attemptCount );
if (attemptCount > 30) {
console.log("Could not fing Gmonkey in the gmail page after thirty seconds. Aborting");
clearInterval(intervalId); // unregister this function for interval execution.
}; …Run Code Online (Sandbox Code Playgroud) javascript gmail xmlhttprequest setinterval google-chrome-extension
我怎样才能使用jQuery + jQuery Mobile.一段时间后弹出一个对话框消失.这是我在某种程度上写的作品.它会在一段时间后消失.但是,当我单击按钮再次激活它时它不起作用,除非我再次刷新页面.
JavaScript的
<script type="text/javascript">
$(document).on('pageinit', function(e) {
$('#postnote').click(function() {
$('#dialog').popup('open', {history: false}).delay(500).fadeOut('slow').hide();
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
这页纸
<div data-role="page" id="addnote">
<div id="dialog" data-role="popup" data-transition="fade">
<div data-role="header"><h1>Note posted</h1></div>
</div>
<div data-role="header" data-theme="a">
<h1>Add Note</h1>
</div>
<div data-role="content" data-theme="b">
<textarea id="note" rows="40" name="note"></textarea>
<a href="#" id="postnote" data-role="button" data-transition="fade" data-theme="b">Post</a>
<a href="#" data-rel="back" data-role="button" data-transition="slidefade" data-theme="a" data-direction="reverse">Back</a>
</div><!-- /content -->
</div><!-- /page -->
Run Code Online (Sandbox Code Playgroud) 我正在为实时产品开发浏览器扩展.我有一个后台页面,在manifest.json中设置了"persistent:true"(我使用的是版本v2).我每隔一秒使用setInterval()轮询服务器以获取新数据.后台脚本还会缓存到目前为止收集的数据,并将其提供给任何新打开的选项卡.
事情很好,直到有时我注意到当我让电脑长时间睡眠时,我对服务器的调查就停止了!如果我刷新了任何现有选项卡,我确实看到了缓存数据.这意味着,后台页面未被Chrome杀死.我的问题是,为什么chrome只是停止setInterval()调用?此外,如果由于某种原因停止投票,恢复投票的正确方法是什么?
//relevant part of manifest.json
"background": {
"scripts": [
"js/background/jquery.min.js",
"js/background/bgconfig.js",
"js/background/backgroundmanager.js",
"js/background/eventsfetcher.js"
],
"persistent": true
},
Run Code Online (Sandbox Code Playgroud)
谢谢!
javascript google-chrome setinterval google-chrome-extension
我想从3减少到0然后在循环中回到3.这是一种"滑块"实现.一切都正常运行,直到到达clearInterval距离counterry.我错过了什么?
var counttx = 0, // counter
counterrx = setInterval(timerrx, 1000), // countup - start
counterry; // countdown after reach 3
function timerrx(){
counttx = counttx+1;
//console.log(counttx);
if(counttx > 2){
counterry = setInterval(timerry, 1000);
clearInterval(counterrx);
}
}
function timerry(){
counttx = counttx-1;
//console.log(counttx);
if(counttx < 2){
setInterval(timerrx, 1000);
clearInterval(counterry);
}
}
Run Code Online (Sandbox Code Playgroud) function iPadMovie(id) {
$(function () {
var i = 1;
var interval = setInterval(function () {
jQuery('.animationMax img').attr({
src: 'http://jdsports.scene7.com/is/image/JDSports/127932jd' + ('0' + i).slice(-2) + '?hei=255&wid=427&resmode=sharp&op_usm=1.1,0.5,0,0&defaultImage=JDSports/sizeImageMissing'
});
i++;
if (i === 28) i = 1;
}, 100);
});
}
function playIpad(){
iPadMovie();
}
function stopIpad(){
clearInterval = interval;
}
Run Code Online (Sandbox Code Playgroud)
你可以在这里看到小提琴:http://jsfiddle.net/Vv2u3/15/我希望能够停止电影并重新启动它,如果他们按下播放.当然我可以在方法之外使用clearInterval吗?
我想每秒更改sqaure的颜色(#myID:width = height = 100px).(为了检查切换循环是否有效,在每个"case"中我写了console.log("smth happen");.) 但是这个方块的颜色不会改变."小提琴"
接下来,每隔一个document.getElementById('myID')被写入一个新形成的变量thesquare.如何使变量全局,在函数之外?
使用Javascript:
var i = 0;
function changecolor()
{
var thesquare = document.getElementById('myID');
switch (i)
{
case 0 :
thesquare.setAttribute("background-color","red");
++i;
break;
case 1 :
thesquare.setAttribute("background-color","green");
++i;
break;
default :
thesquare.setAttribute("background-color","blue");
i=0;
break;
}
}
setInterval("changecolor()",1000);
Run Code Online (Sandbox Code Playgroud) 授予以下代码:
function updateOdometers(odometers) {
setTimeout(function(){
odometers[1].update(odometers[1].value + 10);
}, 500);
}
setInterval(updateOdometers(odometers), 2000);
Run Code Online (Sandbox Code Playgroud)
无论出于何种原因,此代码仅更新里程表的值一次,而不是内部延迟每2000ms更新一次.谷歌搜索/ SO-ing并没有给我带来太多结果.有任何想法吗?
这是一个打字稿类。我正在尝试访问setInterval函数中的开始时间和计数。我之前已经设置了值,为什么我们会得到undefined和NaN?以及如何解决这个问题?
startTimer() {
this.startTime = new Date();
this.count = 100;
console.log("STARTTIME: " + this.startTime); //RESULT: Mon Dec 17..etc
console.log("COUNT: " + this.count); //RESULT: 100
this.timer = setInterval(function () {
console.log("STARTTIME: " + this.startTime); //Undefined
this.count += 1;
console.log("COUNT: " + this.count); //NaN
}, 1000);
}
Run Code Online (Sandbox Code Playgroud) setinterval ×10
javascript ×8
jquery ×4
angular ×1
animation ×1
function ×1
gmail ×1
settimeout ×1
timer ×1
typescript ×1