最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - problem with setTimeout "function is not define" ! - Stack Overflow

programmeradmin1浏览0评论

problem with setTimeout "function is not define" !

What is the problem in this code ?

$(document).ready(function(){ 

 function ISS_NextImage() { //ImageSlideShow NextImage
  $('.ImageSlideShow').each(function() {
   alert($(".correntImage", this).text());
  });
 }

 var t=setTimeout("ISS_NextImage();",1000);

});

problem with setTimeout "function is not define" !

What is the problem in this code ?

$(document).ready(function(){ 

 function ISS_NextImage() { //ImageSlideShow NextImage
  $('.ImageSlideShow').each(function() {
   alert($(".correntImage", this).text());
  });
 }

 var t=setTimeout("ISS_NextImage();",1000);

});
Share Improve this question asked Sep 20, 2010 at 21:36 faressoftfaressoft 19.7k44 gold badges107 silver badges149 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 12

When you eval code, it is done in the global scope. Since the function you are trying to call is locally scoped, this fails.

Pass the function to setTimeout instead of passing a string to be evaled.

var t=setTimeout(ISS_NextImage,1000);

Try changing your set timeout call to this:

var t=setTimeout(function(){ISS_NextImage();},1000);

Avoid passing a string to setTimeout(). Just pass a reference to the function instead:

var t = setTimeout(IIS_NextImage, 1000);

you could also:

$(function() { 
    var t = setTimeout(new function() {
       $('.ImageSlideShow').each(function() {
           alert($(".correntImage", this).text());
       });
    }, 1000);
});

You could do something like this:

$(document).ready(function(){ 
    setTimeout(ISS_NextImage,1000);
});

function ISS_NextImage() { 
    $('.ImageSlideShow').each(function() {
      alert($(".correntImage", this).text());
    });
 }
发布评论

评论列表(0)

  1. 暂无评论