答案 0 :(得分:0)
这里是一段代码,它将从一组搜索短语中随机选择,打开一个新窗口,然后在该窗口中对该短语进行谷歌搜索,然后按一定时间间隔将该窗口更改为一个新的谷歌搜索。您没有说出如何选择随机搜索短语,所以我想办法做到这一点,但您显然可以插入自己的方法。这是代码working demo:
var searchWindow, interval;
var searchPhrases = [
"california earthquake", "desean jackson", "noah",
"louisville basketball", "ultra music festival", "january jones",
"stephen colbert", "taco bell breakfast", "college board",
"zac efron", "johnny manziel", "miguel cabrera", "psych"
];
function getRandomSearchPhrase() {
var rand = Math.floor(Math.random() * searchPhrases.length);
return searchPhrases[rand];
}
function makeRandomGoogleSearchURL() {
var search = encodeURI(getRandomSearchPhrase().replace(/\s/g, "+"));
var url = "https://www.google.com?q=" + search + "#q=" + search;
return url;
}
// create the interval portion to refresh the Google search
// use short time here for purposes of the demo
function startInterval() {
if (interval) {
window.clearInterval(interval);
}
interval = window.setInterval(function() {
if (searchWindow && searchWindow.location) {
// stop the interval if the window has been closed
searchWindow.location = makeRandomGoogleSearchURL();
} else {
window.clearInterval(interval);
searchWindow = null;
}
}, 5000);
}
// hook up the button to start it all
document.getElementById("start").addEventListener("click", function() {
if (!searchWindow || !searchWindow.location) {
searchWindow = window.open(makeRandomGoogleSearchURL(), "myGoogleSearch");
startInterval();
}
})
请参阅此处的工作演示:http://jsfiddle.net/jfriend00/pgkY5/