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

How to check the status each 10 seconds with JavaScript? - Stack Overflow

programmeradmin1浏览0评论

I have a server (mysite/status), which returns number of active tasks (just a single integer). How can I check number of active tasks each 10 seconds with JavaScript and show user something like:

Number of remaining tasks: XXX

And, if number of tasks is 0, then I should load another page.

I have a server (mysite./status), which returns number of active tasks (just a single integer). How can I check number of active tasks each 10 seconds with JavaScript and show user something like:

Number of remaining tasks: XXX

And, if number of tasks is 0, then I should load another page.

Share Improve this question asked Aug 7, 2011 at 8:27 LA_LA_ 20.4k60 gold badges179 silver badges318 bronze badges 5
  • You have to use AJAX - can you get hold of jQuery? If so, it's one line solution and cross browser, otherwise it's big headache, but still possible. – Shadow Wizzard Commented Aug 7, 2011 at 8:32
  • I don't use jQuery. Can it be XMLHttpRequest? – LA_ Commented Aug 7, 2011 at 9:07
  • Yes but making it cross browser will be headache. I have old code somewhere, untested for long time - want me to post it anyway? – Shadow Wizzard Commented Aug 7, 2011 at 9:10
  • @Shadow Wizard, If I would agree to reload the page each 10 secs, will it be much more easy? – LA_ Commented Aug 7, 2011 at 9:17
  • I don't think it's relevant - you still need to grab the data from different page, right? – Shadow Wizzard Commented Aug 7, 2011 at 9:29
Add a ment  | 

5 Answers 5

Reset to default 5

Make a function set a new timeout calling itself.

function checkTasks(){
    // Make AJAX request
    setTimeout(checkTasks, 10000);
}
checkTasks(); // Start task checking

with jQuery for AJAX functions... (untested code)

setInterval(function(){
  $.get('http://example./status',function(d){
    // where there is html element with id 'status' to contain message
    $('#status').text('Number of remaining tasks: '+d)
    if(d == 0){
      window.location = '/another/page'
    }
  })
},10000)

I have thought of different approach, without any AJAX. As you just need to show simple plain data, just use <iframe> to show that dynamic data then with simple JS "reload" the frame every 10 seconds.

HTML would be:

Number of remaining tasks: <iframe id="myFrame" src="mysite./status"></iframe>

And the JavaScript:

window.onload = function() {
    window.setTimeout(ReloadTime, 10000);
};

function ReloadTime() {
    var oFrame = document.getElementById("myFrame");
    oFrame.src = oFrame.src;
    window.setTimeout(ReloadTime, 10000);
}

Live test case, using time for the same of example.

With some CSS you can make the frame look like part of the text, just set fixed width and height and remove the borders.

 setInterval(function(elem){     // here elem is the element node where you want to display the message
        var status=checkStatus();  // supposing that the function which returns status is called checkStatus
        if(status == 0){
          window.location = '/another/page'
        }
        else {
         elem.innerHTML="Number of remaining tasks:"+status;
        }
    },10000)

Using the javascript library jquery, you can set a repeating infinite loop. That gets the data from a page and then sets the inner html of an element.

http://jsfiddle/cY6wX/14/ This code is untested

edit: demonstration updated Also I did not use the jquery selector for setting the value in case you do not want to use jquery.

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>